php Zend_Form -> 很好地改变 setRequired() 验证消息

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/459921/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 22:50:05  来源:igfitidea点击:

Zend_Form -> Nicely change setRequired() validate message

phpzend-frameworkzend-form

提问by dittonamed

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:

自定义消息?我在某处读到要替换消息的内容,只需使用 addValidator(...,而不是 (NO setRequired),如下所示:

$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 twoerror messages.

但在我的测试中,这不起作用 - 它根本没有验证 -它将通过一个空文本字段。同时使用 (addValidator('NotEmp.. + setRequired(true))也不起作用 - 它双重验证,给出两条错误消息。

Any ideas?

有任何想法吗?

Thanks!

谢谢!

回答by dittonamed

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

设置此“站点范围”的更简单方法可能是在引导程序或基本 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);
?>

回答by typeoneerror

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.

关键是验证器上的“真”,如果你将它设置为真,它会在它之后杀死其他验证。如果您添加多个验证方法,但将其设置为 false,它将验证所有方法。

回答by Jose Arandi

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

Zend_Form 将所需的验证错误设置为“isEmpty”,因此您可以使用 setErrorMessages() 覆盖其消息。例如:

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

It worked for me, using ZF 1.11

它对我有用,使用 ZF 1.11

回答by typeoneerror

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.

这允许您“获取”验证器,然后“设置”它的属性。

回答by typeoneerror

Try the following.

请尝试以下操作。

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

This worked form me.

这对我有用。

回答by 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.

这仅为 Digits 验证器设置自定义错误消息。

回答by armandfp

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.

为了获得验证的正确行为。

回答by dittonamed

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

如果留空并提交,它会给出两条消息bar& ' ' is a empty string。这是来自 setRequired(true) 的第二条消息,这就是问题所在

回答by dittonamed

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?

如果任一验证失败,将生成两个错误消息。第一次失败后不应该停止吗?

回答by max4ever

use a zend translator with zend_validate.phpfrom

使用Zend的翻译与zend_validate.php

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

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

然后根据您的需要对其进行相应修改