Zend Form ViewScript how to show error messages

3.1k Views Asked by At

After days of trying to get this to work I am turning to you. I am rendering a custom form with the ViewScript decorator and can't get the error messages to be shown. I have tried many different solutions that I have found here but getErrorMessages() is always empty.

This is my login form:

    public function init() {
    $this->setDecorators(array('Errors', array('ViewScript', array('viewScript' => '_forms/_login.phtml') ) ) );

    $this->setName('loginform');$username = new Zend_Form_Element_Text('username');
    $username->setAttrib('size', '35');
    $username->setRequired(true);
    $username->addValidator('NotEmpty');
    $username->setDecorators( array('ViewHelper',  'Errors') );

    $password = new Zend_Form_Element_Password('password');
    $password->setRequired(true);
    $password->addValidator('NotEmpty');
    $password->setDecorators( array('ViewHelper',  'Errors') );

    $submit = new Zend_Form_Element_Button('submit');
    $submit->setAttrib('type', 'submit');
    $submit->setDecorators( array('ViewHelper',  'Errors') );

    $this->setMethod('post'); 
    $this->setAction('/auth/index');

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

This is my viewscript:

<form action="<?php echo $this->element->getAction() ?>" name="<?php echo $this->element->getName() ?>"  method="<?php echo $this->element->getMethod() ?>" id="login-form">
<ul class="nav">
<li><?php echo $this->element->username; ?></li>
<li><?php echo $this->element->password; ?></li>
<li><?php echo $this->element->submit; ?></li>
</ul>

And this is my processing action:

 public function indexAction()
{
    $request = $this->getRequest();
    $form = new Form_LoginForm();
    if ($request->isPost()){
        if($form->isValid( $this->_request->getPost() )) {  //This part works

        }
        else {

        }       
    }
}

Tried the example seen here without success.

Any ideas?

1

There are 1 best solutions below

0
On BEST ANSWER

Try this, you can put the error messages wherever you want them to be printed.

<li><?php echo $this->element->username .
 $this->formErrors($this->element->username->getMessages() ); ?></li> 
<li><?php echo $this->element->password. 
$this->formErrors($this->element->password->getMessages() ); ?></li> 
<li><?php echo $this->element->submit; ?></li>