Reputation: 18006
I have created a zend form. I have added some elements. I have put validate attribute in all elements but validation in not working out. Here is my code
/* Form Elements & Other Definitions Here ... */
$this->setMethod('post');
$this->addElement('text', 'email', array(
'required' => true,
'filters' => array('StringTrim'),
'validators' => array(
'EmailAddress',
)
));
// Add the comment element
$this->addElement('textarea', 'comment', array(
// 'label' => 'Please Comment:',
'required' => true,
'validators' => array(
array('validator' => 'StringLength', 'options' => array(0, 20))
)
));
// Add a captcha
$this->addElement('captcha', 'captcha', array(
//'label' => 'Please enter the 5 letters displayed below:',
'required' => true,
'captcha' => array(
'captcha' => 'Figlet',
'wordLen' => 5,
'timeout' => 300
)
));
// Add the submit button
$this->addElement('submit', 'submit', array(
'ignore' => true,
'label' => 'Sign Guestbook',
));
// And finally add some CSRF protection
$this->addElement('hash', 'csrf', array(
'ignore' => true,
));
Not even a single validation in working out. On my view I just echo
the form object. Would any body tell me where Am I Wrong?
Upvotes: 1
Views: 2013
Reputation: 2673
Zend_Form
is supposed to validate your input in the server side. To use it, you need to send the data to the server somehow, and then call $form->isValid($data)
.
The simple way would be just to submit the form, and validate the data in your action, there are some good tutorials about it, for instance this one: http://akrabat.com/zend-framework-tutorial/ .
If you want to leverage the validators but still want to get some validation feedback before submitting the form, you can also send the data through Ajax. The following tutorial tells you a nice way to do it using jQuery: http://www.zendcasts.com/ajaxify-your-zend_form-validation-with-jquery/2010/04/ . I followed it and it was quite good.
In any case, keep in mind that the validation is always run in the server side.
Hope that helps,
Upvotes: 5