How to Validation Zend Form ? (opening tag)
Example :
Form :
class MyForm extends Zend_Form {
function init() {
$this->addElement('select','my_select',array(
'label'=>'My select :',
'required'=>true,
'multioptions'=>array(''=>'-select please-','1'=>'value1','2'=>'value2')
'validators'=>array(
array('NotEmpty', true, array('messages' => 'This field is required'))),
));
} }
Controller :
$form = new MyForm();
if ($this->_request->isPost()) {
$form_name=$this->getRequest()->getParams();
if($form->isValid($form_name)){开发者_如何学JAVA
echo "==success==";
}
else{
echo "==no success==";
}
}
$this->view->form = $form;
View :
// Render the form opening tag
echo $this->form->renderForm(false);
echo '<table>';
echo '<tr>';
echo '<th>'
// Render the label
echo $this->form->my_select->renderLabel();
echo '</th>
echo '<td>';
// Render the select
echo $this->form->my_select->renderViewHelper();
echo $this->form->my_select->renderErrors();
echo '</td>';
echo '</tr>';
echo '</table>';
echo '</form>';
i want to show message validation at view
This code is complete in answer 18/02/2011
You can render errors on a specific element with:
echo $this->form->my_select->renderErrors();
If you want to render the errors for all the form in one place :
$form->addDecorator('FormErrors');
echo $form->renderFormErrors();
I'm not really sure what you mean here but here's my best guess...
If you want to render the set of validation errors for the form and its elements in one place, try adding the FormErrors
decorator to the form. See Zend_Form_Decorator_FormErrors
As for validation, simply add validators to the elements as normal.
to have validation errors you first need to add validators to your form element ,
$formElement = new Zend_Form_Element_Text('username');
$formElement->addValidator(new Zend_Validate_Alnum());
to get validation error messages do
$arrayOfErrors = $this->view->form->getMessages();
精彩评论