Zend Framework: Errors are not visible/displayed
I have following form class:
$username = new Zend_Form_Element_Text('username');
$username-开发者_如何学Go>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')),
));
And following is Action Controller code:
$form = new Application_Form_Adminlogin();
$this->view->form = $form;
If I submit blank username field, then no error shows on the page. Can some one guide me what Iam doing wrong and how it can be rectified.
Thanks in advance
Zend_Form does not automatically populate/validate itself agaist GET/POST variables.
As noted in the comments, you should do this:
$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;
This should also show errors when the form displayed after incorrect parameters were input.
精彩评论