Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Zend Form: add error message after form validation

How to add an error message to Zend Form element after the form was already validated?

I'm trying to add error mesages I get from Zend_Auth (now I'm displaying them using flashMessenger).

I tried something like this:

$form->getElement('username')->addErrorMessage('my message');
like image 525
takeshin Avatar asked Mar 23 '10 13:03

takeshin


2 Answers

From the zend form documentation -

addErrorMessage($message): add an error message to display on form validation errors. You may call this more than once, and new messages are appended to the stack.

addError($message): add a message to the custom error messages stack and flag the form as invalid.

If your form is not marked as invalid, it probably doesn't show any error messages. Using addError($message) rather than addErrorMessage($message) will ensure that the element is also marked invalid.

like image 119
thetaiko Avatar answered Oct 04 '22 18:10

thetaiko


if(!$your_zend_auth_result){
    $form->getElement('username')->addError('Your Message');
    $form->markAsError();
}
like image 43
Andrei Stalbe Avatar answered Oct 04 '22 16:10

Andrei Stalbe