|
|||
|
Hi,
I'm having some issue with Zend_Filter_Input and wondered if anyone could help me? All I'm trying to do is have a form submitted, filter/validate it and then if it's not valid render the form again with the filtered data populated in the form elements, but I must be doing something stupid which is probably blindingly obvious to everyone except to me! :-) My action looks like: Code:
public function registerAction()
{
if ($this->_request->isPost()) {
$filters = array(
'*' => array('StringTrim', 'StripTags', new Component_View_Filter_StripSlashes())
);
$validators = array(
'email' => array(
'EmailAddress',
'allowEmpty' => false
),
'password' => array(
array('StringLength', 6),
'allowEmpty' => false
),
'confirm_password' => array(
'StringEquals',
'fields' => array('confirm_password', 'password'),
'allowEmpty' => false
),
'username' => array(
'Alnum',
array('StringLength', 6, 32),
'allowEmpty' => false
)
);
$input = new Zend_Filter_Input($filters, $validators, $this->_request->getPost());
$input->addNamespace('Component_Validate');
if ($input->isValid()) {
echo "Form OK<br />\n";
} else {
$messages = $input->getMessages();
$this->view->invalid = $messages;
}
Zend_Debug::dump($this->_request->getPost());
Zend_Debug::dump($input);
Zend_Debug::dump($input->address2);
$this->view->post = $input;
}
$this->view->countries = $this->_countries;
}
Code:
array(14) {
["username"] => string(0) ""
["email"] => string(0) ""
["password"] => string(0) ""
["confirm_password"] => string(0) ""
["firstname"] => string(7) " foo "
["lastname"] => string(15) "bar "
["address1"] => string(21) "<script>ack!</script>"
["address2"] => string(0) ""
["city"] => string(22) " it\'s a test"
["county"] => string(0) ""
["postalcode"] => string(0) ""
["country"] => string(2) "GB"
["site_contact"] => string(1) "1"
["partner_contact"] => string(1) "1"
}
object(Zend_Filter_Input)#56 (13) {
["_data:protected"] => array(10) {
["firstname"] => string(3) "foo"
["lastname"] => string(3) "bar"
["address1"] => string(4) "ack!"
["address2"] => string(0) ""
["city"] => string(11) "it's a test"
["county"] => string(0) ""
["postalcode"] => string(0) ""
["country"] => string(2) "GB"
["site_contact"] => string(1) "1"
["partner_contact"] => string(1) "1"
}
...
}
NULL
Do the filtered values only get returned if the form is valid? Also, in my view I have something like this: Code:
<?php echo $this->formText('address2', $this->post->address2, array('id' => 'frm_address2', 'maxlength' => 48)); ?>
Any help or advice would be truly appreciated!! Thanks, Andy PS: I posted instead of Andy amnuts because he had some problems posting the code. |
|
|||
|
The ever-helpful naneau has just pointed out on #zftalk that a validator has to exist for all form elements.
So by simply adding this to my $validators: '*' => array() I default all elements to use an empty validator. Problem solved! |
|
|||
|
I noticed in your code you are using the "StringEquals" validator. Would you mind sharing how you implemented this (eg where your class file is and how it's loaded/extended to work in your controller). I'd really appreciate it. Thanks!
|
![]() |
| Thread Tools | |
| Display Modes | |
|
|