如何验证Zend表格? (打开标签)
示例:
表格:
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'))),
));
} }
控制器:
$form = new MyForm();
if ($this->_request->isPost()) {
$form_name=$this->getRequest()->getParams();
if($form->isValid($form_name)){
echo "==success==";
}
else{
echo "==no success==";
}
}
$this->view->form = $form;
查看:
// 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>';
我想在视图中显示消息验证
此代码在回答18/02/2011
时已完成答案 0 :(得分:0)
我不太确定你的意思,但这是我最好的猜测......
如果要在一个位置呈现表单及其元素的验证错误集,请尝试将FormErrors
装饰器添加到表单中。见Zend_Form_Decorator_FormErrors
至于验证,只需像往常一样向元素添加验证器。
答案 1 :(得分:0)
要获得验证错误,首先需要在表单元素中添加验证器,
$formElement = new Zend_Form_Element_Text('username');
$formElement->addValidator(new Zend_Validate_Alnum());
获取验证错误消息
$arrayOfErrors = $this->view->form->getMessages();
答案 2 :(得分:0)
您可以使用以下内容在特定元素上呈现错误:
echo $this->form->my_select->renderErrors();
如果您想在一个地方呈现所有表单的错误:
$form->addDecorator('FormErrors');
echo $form->renderFormErrors();