问题描述
假设我创建了一个这样的文本元素:
Say I create a text element like this:
$firstName = new Zend_Form_Element_Text('firstName');
$firstName->setRequired(true);
更改默认错误消息的最佳方法是:
值为空,但非空值必填
自定义消息?我在某处读到要替换消息,只需使用 addValidator(..., 代替 (NO setRequired),如下所示:
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'));
但在我的测试中,这不起作用 - 它根本无法验证 - 它将通过一个空文本字段. 同时使用 (addValidator('NotEmp.. + setRequired(true)) 同时也不起作用 - 它双重验证,给出两条错误消息.
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.
有什么想法吗?
谢谢!
推荐答案
设置此站点范围"的更简单方法可能是在引导程序或基本 zend_controller 中执行以下操作:
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);
?>
这篇关于Zend_Form ->很好地更改 setRequired() 验证消息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!