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;
}
The above debug output shows me that the form is being submitted OK, and the filters are being applied. But when I try to access the input value with the getter, I just get a null value:
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
Anyone know why this might be? I've also tried to use $input->getEscaped() but without any luck.
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)); ?>
Which produces an empty element on the submit (because of the above issue), and an error prior to form submission because the post view variable doesn't exist yet. Obviously I could wrap the $this->post->address2 in a ternary statement, or something, to default the value, but I wondered what best practise on this was? Remembering that I want to display the filtered values again and not the raw ones that I could with $_POST.
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.