Zend validators and error messages: addValidator and addErrorMessage
Asked Answered
S

4

5

If I have a form element that has multiple validators attached to it (3 in this example), how would I use addErrorMessage to 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()
Ssm answered 2/10, 2010 at 7:44 Comment(0)
D
11

Typically it's done per validator error message, not per validator...

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

But I often prefer to override all of an element's errors to a single

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

or, if I need it per validator, this works...

$validator->setMessages('string error')

should override all a validator's errors to a single message. I could not find this documented anywhere, but it works for me. Because of this, it may not work for all versions?

To me, the error messaging handling is a bit messy unless you want to override every possible error message, but hopefully one of these solutions works for you.

Cheers

Daedalus answered 2/10, 2010 at 17:38 Comment(1)
i have some aditional errors but using the first validation not workingPetta
P
6

Add your message along with the validator as below. Example:

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

->addValidator('NotEmpty', true, array('messages'=>'Cannot be empty'))
Perennial answered 16/12, 2011 at 19:7 Comment(0)
L
2

Add your message like this

->addValidator( 'Alpha', true, array( 'messages' => array( 'notAlpha' => "Please enter alphabetic character only in Product name.
") ));

Add validator message according to your error string

Lizarraga answered 13/2, 2013 at 7:3 Comment(2)
I like the specified message handling via 'notAlpha'. can you guide me to where I can find more of those identifiers for the rest of validators? thanks anyway :^)Parrisch
You find these varibale of error message from its validate file. go to below path project/library/Zend/Validate select your validate rule, Here you find list of "const" that is for error messageLizarraga
R
0

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

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

Hope it helps!!

Renewal answered 24/12, 2013 at 9:57 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.