我已使用此答案中的验证器错误自定义示例:https ://stackoverflow.com/a/4881030/822947 ,但我需要使用 addValidator() 或 setMessage()/setMessages() 覆盖一些翻译错误。下面的例子不起作用(似乎,内置的翻译器有优先权?)......我该怎么做?
$field->addValidator ('Alpha', false, array ('messages' => array (Zend_Validate_Alpha::NOT_ALPHA => 'My msg')));
$Alpha = new Zend_Validate_Alpha ();
$Alpha->setDisableTranslator (true);
$Alpha->setMessage ('My msg', Zend_Validate_Alpha::NOT_ALPHA);
$Alpha->setMessages (array (Zend_Validate_Alpha::NOT_ALPHA => 'My msg'));
$field->addValidator ($Alpha);
更新 问题不在于我添加验证器并向其设置消息的方式。
我的目标是本地化所有错误消息。但是对于某些表单字段,我需要添加更具体的消息。
例如,在我的 lang/translate.php 我有 common
Zend_Validate_Alpha::NOT_ALPHA => 'Value contains non alphabetic characters'
,
但对于名称字段,我需要更具体
Zend_Validate_Alpha::NOT_ALPHA => 'Field can contain only alphabetic characters and spaces'
,
当我启用内置翻译器时,问题是特定消息被忽略。所以例如代码
$form = new Zend_Form ();
$validator = new Zend_Validate_Alpha ();
$validator->setMessages (array (
Zend_Validate_Alpha::NOT_ALPHA => 'xxx %value% x'
));
$form->addElement ('text', 'digit', array (
'validators' => array (
$validator
)
));
$name = new Zend_Form_Element_Text ('name');
$name->addValidator ('Alpha', true, array (
'allowWhiteSpace' => true,
'messages' => array
(
Zend_Validate_Alpha::NOT_ALPHA => 'my more specific localized msg',
)
));
$form->addElement ($name);
$form->isValid (array (
'digit' => '___',
'name' => '___',
));
Zend_Debug::dump ($form->getMessages ());
当翻译器禁用时,我有
array(2) {
'digit' =>
array(1) {
'notAlpha' =>
string(9) "xxx ___ x"
}
'name' =>
array(1) {
'notAlpha' =>
string(30) "my more specific localized msg"
}
}
启用翻译器后,我收到来自 lang/translate.php 的消息
array(2) {
'digit' =>
array(1) {
'notAlpha' =>
string(104) "common localized msg"
}
'name' =>
array(1) {
'notAlpha' =>
string(104) "common localized msg"
}
}