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?
如果我有一个带有多个验证器的表单元素(本例中为3),当每个唯一的验证器失败时,我如何使用addErrorMessage来创建自定义错误消息。是否有方法为每个验证器添加自定义消息?
$element = new Zend_Form_Element_Text()...
$element->....
->addValidator(...)
->addValidator(...)
->addValidator(...)
->addErrorMessage()
4 个解决方案
#1
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
干杯
#2
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'))
#3
2
Add your message like this
像这样添加您的消息。
->addValidator( 'Alpha', true, array( 'messages' => array( 'notAlpha' => "Please enter alphabetic character only in Product name.
") ));
->addValidator('Alpha', true, array) ('messages' => array('notAlpha' => ")请只在产品名称中输入字母字符。")));
Add validator message according to your error string
根据错误字符串添加验证器消息
#4
0
addErrorMessage('Your Custom Message'); It is also easiest way to print the custom message.
addErrorMessage(您的自定义消息);这也是打印自定义消息的最简单方法。
The addErrorMessage is defined inside libraray/zend/Form/Elements.php
addErrorMessage在libraray/zend/Form/Elements.php中定义。
Hope it helps!!
希望它帮助! !
#1
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
干杯
#2
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'))
#3
2
Add your message like this
像这样添加您的消息。
->addValidator( 'Alpha', true, array( 'messages' => array( 'notAlpha' => "Please enter alphabetic character only in Product name.
") ));
->addValidator('Alpha', true, array) ('messages' => array('notAlpha' => ")请只在产品名称中输入字母字符。")));
Add validator message according to your error string
根据错误字符串添加验证器消息
#4
0
addErrorMessage('Your Custom Message'); It is also easiest way to print the custom message.
addErrorMessage(您的自定义消息);这也是打印自定义消息的最简单方法。
The addErrorMessage is defined inside libraray/zend/Form/Elements.php
addErrorMessage在libraray/zend/Form/Elements.php中定义。
Hope it helps!!
希望它帮助! !