Zend_Form -> Nicely change setRequired() validate

2019-03-08 19:45发布

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!

10条回答
女痞
2楼-- · 2019-03-08 19:50

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

查看更多
爷、活的狠高调
3楼-- · 2019-03-08 19:51

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

查看更多
\"骚年 ilove
4楼-- · 2019-03-08 19:58

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.

查看更多
Juvenile、少年°
5楼-- · 2019-03-08 19:59

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.

查看更多
一纸荒年 Trace。
6楼-- · 2019-03-08 20:01

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.

查看更多
甜甜的少女心
7楼-- · 2019-03-08 20:08

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

查看更多
登录 后发表回答