Zend Form: add error message after form validation
Asked Answered
W

3

13

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');
Westwardly answered 23/3, 2010 at 13:13 Comment(1)
Other errors are displayed as expected.Westwardly
N
12

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.

Nari answered 23/3, 2010 at 13:44 Comment(1)
Sorry, There was a typo in my code. $form->getElement('username')->addErrorMessage('my message'); works as expected.Westwardly
P
5
if(!$your_zend_auth_result){
    $form->getElement('username')->addError('Your Message');
    $form->markAsError();
}
Proteolysis answered 2/2, 2013 at 13:13 Comment(0)
P
1

You need to use setErrors() method to create errors stack. In the case, when element already has some errors you should use addErrors() method. To check if element has errors you can use hasErrors() method

Plug answered 28/9, 2011 at 11:58 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.