Zend Framework:错误不可见/显示

时间:2011-07-13 12:18:17

标签: zend-framework zend-form zend-form-element

我有以下表单类:

$username = new Zend_Form_Element_Text('username');
$username->setLabel('Username:')
      ->setRequired(true)
      ->addValidator('NotEmpty')
      ->addFilter('StringTrim')
      ->setAttribs(array('class' => 'textfield'));

$submit = $this->createElement('submit', 'mySubmit');
$submit->setLabel('Login')
       ->setAttribs(array('id' => 'btnsubmit', 'class' => 'btn'));

$reset = $this->createElement('reset', 'myReset');
$reset->setLabel('Reset')
      ->setAttribs(array('id' => 'btnreset', 'class' => 'btn'));

$this->addElements(array($username, $submit, $reset));

$this->setElementDecorators(array(
    'ViewHelper',
    'Errors',
    array('Label', array('class' => 'login_label', 'placement'=> 'REPLACE')),
    array('HtmlTag', array('tag'=> 'p')),    
));

 $this->setDecorators(array(
     'FormElements',
     'Form',             
     array('Fieldset', array('legend' => 'Login Info')), 
     array(array('mydiv' => 'HtmlTag'), array('tag' =>'div', 'id' => 'lgrd')),
 ));

以下是Action Controller代码:

$form = new Application_Form_Adminlogin();
$this->view->form = $form;

如果我提交空白用户名字段,则页面上不会显示错误。有人可以指导我做错了什么以及如何纠正。

提前致谢

1 个答案:

答案 0 :(得分:0)

Zend_Form不会自动填充/验证自己的GET / POST变量。

如评论中所述,您应该这样做:

$postData = $this->getRequest()->getPost();
if($form->isValid($postData)) {
  // Do what you need to do when form is valid
} else {
  // Do what you need to do when form is NOT valid
}
$this->view->form = $form; 

当输入错误参数后显示的表单时,这也应该显示错误。