Zend validators and error messages: addValidator and addErrorMessage

If I have a form element that is attached to it by several validators (3 in this example), how would I use it addErrorMessageto create custom error messages when each unique validator fails. Is there a way to add a custom message for each validator?

$element = new Zend_Form_Element_Text()...
$element->....
        ->addValidator(...)
        ->addValidator(...)
        ->addValidator(...)
        ->addErrorMessage()
+5
source share
4 answers

This is usually done to report a validator error, not a validator ...

$element->setErrorMessages(array(Zend_Validate_...::CONSTANT => 'New Message'));

But I often prefer to redefine all element errors to one

$element->setErrorMessages(array('Single Error'));

or, if I need it on a validator, it works ...

$validator->setMessages('string error')

. , . - ?

, , , , .

+11

, .  :

->addValidator('StringLength', false, array(0,255,'messages'=>'Cannot be more than 255 chars'))

->addValidator('NotEmpty', true, array('messages'=>'Cannot be empty'))
+6

→ addValidator ('Alpha', true, array ('messages' = > array ('notAlpha' = > ", .
" )));

+2
source

addErrorMessage ('Your custom message'); It is also the easiest way to print a custom message.

The value of addErrorMessage is defined inside libraray / zend / Form / Elements.php

Hope this helps!

0
source

All Articles