Zend_Form -> Nicely change setRequired() validate message
Asked Answered
W

10

26

Say I create a text element like this:

$firstName = new Zend_Form_Element_Text('firstName');
$firstName->setRequired(true);

Whats the best way to change the default error message from:

Value is empty, but a non-empty value is required

to a custom message? I read somewhere that to replace the message, just use addValidator(..., instead (NO setRequired), like this:

$firstName = new Zend_Form_Element_Text('firstName');
$firstName->addValidator('NotEmpty', false, array('messages'=>'Cannot be empty'));

but in my testing, this doesn't work - it doesn't validate at all - it will pass with an empty text field. Using both (addValidator('NotEmp.. + setRequired(true)) at the same time doesn't work either - it double validates, giving two error messages.

Any ideas?

Thanks!

Wachter answered 20/1, 2009 at 2:10 Comment(2)
Your findings are contrary to mine. $foo->setRequired(true)->addValidator('NotEmpty', false, array('messages' => 'bar')); works as expected, no double messages.Linton
ill post a new "Answer" in response to this because the my comment is apparently too longWachter
R
19

An easier way to set this "site-wide" would be to possibly do the following in a bootstrap or maybe a base zend_controller:

<?php    
$translateValidators = array(
                        Zend_Validate_NotEmpty::IS_EMPTY => 'Value must be entered',
                        Zend_Validate_Regex::NOT_MATCH => 'Invalid value entered',
                        Zend_Validate_StringLength::TOO_SHORT => 'Value cannot be less than %min% characters',
                        Zend_Validate_StringLength::TOO_LONG => 'Value cannot be longer than %max% characters',
                        Zend_Validate_EmailAddress::INVALID => 'Invalid e-mail address'
                    );
    $translator = new Zend_Translate('array', $translateValidators);
    Zend_Validate_Abstract::setDefaultTranslator($translator);
?>
Redroot answered 20/1, 2009 at 18:25 Comment(8)
Yeah but the link of errors is huge and will probably grow. Hopefully the custom error interface will change. It's too granular which would be ok if the default error messages were usable to present to users who aren't engineers.Elson
Disagres with @joedevon, and thanks for posting this John B! You saved me much hassle. I think this is the right approach - the list of error messages is finite and there are only a handful that come up in normal use; all you do is add stuff to this array whenever you see a message that is particularly stupidly written. Why they couldn't have written error messages that sounded sensible in the first place I cannot fathom!Uptotheminute
@Flubba You feel that way because you haven't experienced what I have. Email errors cascading to host errors due to dependencies etc. etc... And some errors you won't necessarily see in testing... I went through a lot of hassle trying this approach. But give it a shot if you want.Elson
@Elson - re-reading your initial comment, I see we concur on the fatuous default messages! Thanks for the additional detail on your experiences, will take note.Uptotheminute
Excellent answer. Solved my custom message issues in one fell swoop. Thanks.Trimetallic
This is the best answer I could find in all the Internet.Semifinal
this is an amazing response. Surprized it doesn't have more upvotesReptant
While this works, sadly it ignores built in translations, so can't fix only part of messages! Shame that there is no addTranslator method, or add translations...Machiavellian
J
16

Give this a shot:

$firstName = new Zend_Form_Element_Text('firstName');
$firstName->setLabel('First Name')
          ->setRequired(true)
          ->addValidator('NotEmpty', true)
          ->addErrorMessage('Value is empty, but a non-empty value is required.');

The key is that "true" on the validator if you set that to true, it'll kill the other validations after it. If you add more than one validation method, but set that to false, it will validate all methods.

Josettejosey answered 20/1, 2009 at 2:30 Comment(1)
great! addErrorMessage is what i was looking for. Though i was able to take out the "->addValidator('NotEmpty', true)" line. Thank youWachter
R
10

Zend_Form sets the required validation error as 'isEmpty', so you can override its message using setErrorMessages(). For example:

//Your Required Element
$element->setRequired(true)->setErrorMessages(array(
'isEmpty'=>'Please fill this field'
));

It worked for me, using ZF 1.11

Reneta answered 14/11, 2012 at 4:58 Comment(1)
It works in a sense that it makes form element return this message. But the same message is returned even if a different validator failed.Dirichlet
J
9

Try

->addValidator('Digits', false);

or

->addValidator('Digits');

You assume that to check Digits it has to have a string length anyway.

Also, I like to do some custom error messages like this:

$firstName->getValidator('NotEmpty')->setMessage('Please enter your first name');

This allows you to "get" the validator and then "set" properties of it.

Josettejosey answered 20/1, 2009 at 2:58 Comment(5)
Your second suggestion works perfect - will change the message nicely and does what its supposed to. Thanks againWachter
One quirk worth mentioning... even though setRequired(true) is supposed to use NotEmpty class behind the scenes, you can't use getValidator('NotEmpty') if setRequired() is used alone without setValidator('NotEmpty... For the love of god, what a strange interfaceWachter
Moral of the story - Don't change the default error messages if your validating empty sets and others at the same time.Wachter
Re: your second comment. That's because the setRequired() adds the NotEmpty validator at the very last minute, i.e. when calling isValid().Linton
bummer. too bad this wont work: $firstName->setRequired(true, false, array('messages'=>'Must contain only letters'));Wachter
R
5

Try the following.

$subjectElement->setRequired(true)->addErrorMessage('Please enter a subject for your message');

This worked form me.

Retroflex answered 12/8, 2009 at 9:33 Comment(0)
W
1

But try this:

$firstName->setRequired(true)
          ->addValidator('NotEmpty', false, array('messages' => 'bar'))
          ->addValidator('Alpha', false, array('messages'=>'Must contain only letters'));

If left empty and submitted, itll give two messages bar & '' is an empty string. Its that second message thats coming from setRequired(true) thats the problem

Wachter answered 20/1, 2009 at 18:20 Comment(0)
P
1

Try this..

$ausPostcode = new Zend_Form_Element_Text('aus_postcode'); $ausPostcode->setLabel('Australian Postcode')
->setRequired(true)
->addValidator('StringLength', false, array(4, 4))
->addValidator(new Zend_Validate_Digits(), false)
->getValidator('digits')->setMessage('Postcode can only contain digits');

This sets the custom error message only for the Digits validator.

Predicament answered 26/10, 2009 at 4:7 Comment(0)
S
1

if you put:

$element->setRequired(false);

the validations don't work at all, you have to define:

$element->setAllowEmpty(false);

in order to get the correct behavior of the validations.

Sphygmo answered 11/8, 2010 at 13:30 Comment(0)
W
0

One small issue. This code:

$zipCode->setLabel('Postal Code')
        ->addValidator('StringLength', true, array( 5, 5 ) )
        ->addErrorMessage('More than 5')
        ->addValidator('Digits', true)
        ->addErrorMessage('Not a digit');

Will generate both error messages if either validation fails. Isn't is supposed to stop after the first fails?

Wachter answered 20/1, 2009 at 2:49 Comment(4)
From what I've read, addErrorMessage() adds a general validation error message. It will appear regardless of which validator failedLinton
yeah that seems to be the case. i was hoping setting the second parameter to true in addValidator would prevent it from showing the next errors, but it didntWachter
If that's true it could solve a problem I'm having...but can I set it from a form.ini file somehow?Elson
You get both message because you are attaching the messages to the queue for the single element, even with the chain break. Try setting the message on the validator, not the element. See bit.ly/d8vNBJBedside
E
0

use a zend translator with zend_validate.php from

ZendFramework-1.11.3\resources\languages\en\Zend_Validate.php and then modify this file how you need

and then modify it accordingly to your needs

Exposition answered 22/3, 2011 at 12:3 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.