0

我有以下表单类:

$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')),
 ));

以下是动作控制器代码:

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

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

提前致谢

4

1 回答 1

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; 

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

于 2011-07-13T13:45:32.320 回答