如何在 Zend_Form_Element 上设置 NotEmpty 验证器的消息? [英] How to set the message of the NotEmpty validator on a Zend_Form_Element?
本文介绍了如何在 Zend_Form_Element 上设置 NotEmpty 验证器的消息?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个根据需要设置的表单元素:
$this->addElement('text', 'email', array('标签' =>'电子邮件地址:','必需' =>真的));
由于我将 required 设置为 true,因此可以确保它不为空.默认错误消息如下所示:
"值为必填项,不能为空"
我尝试在验证器上设置消息,但这会引发致命错误:
$validator = $this->getElement('email')->getValidator('NotEmpty');$validator->setMessage('请输入您的电子邮件地址.');
<块引用>
在非对象上调用成员函数 setMessage()
如何将消息设置为自定义错误消息?
解决方案
您需要覆盖/指定 NotEmpty-validator 而不是使用默认验证器:
$this->addElement('text', 'email', array('标签' =>'电子邮件地址:','必需' =>真的,'验证器' =>大批 ('NotEmpty' =>大批 ('验证器' =>'不是空的','选项' =>大批 ('消息' =>'您的自定义错误信息')))));
I have a form element that I'm setting as required:
$this->addElement('text', 'email', array(
'label' => 'Email address:',
'required' => true
));
Since I'm setting required to true, it makes sure that it's not empty. The default error message looks like this:
"Value is required and can't be empty"
I tried setting the message on the validator, but this throws a fatal error:
$validator = $this->getElement('email')->getValidator('NotEmpty');
$validator->setMessage('Please enter your email address.');
Call to a member function setMessage() on a non-object
How can I set the message to a custom error message?
解决方案
You need to overwrite / specify the NotEmpty-validator instead of using the default one:
$this->addElement('text', 'email', array(
'label' => 'Email address:',
'required' => true,
'validators' => array (
'NotEmpty' => array (
'validator' => 'NotEmpty',
'options' => array (
'messages' => 'YOUR CUSTOM ERROR MESSAGE'
)
)
)
));
这篇关于如何在 Zend_Form_Element 上设置 NotEmpty 验证器的消息?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文