Zend_Form -> Nicely change setRequired() validate message

筅森魡賤 提交于 2019-12-02 17:09:21

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);
?>

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.

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

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.

Try the following.

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

This worked form me.

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

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.

JP.

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.

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?

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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!