我有一个我根据需要设置的表单元素:
$this->addElement('text', 'email', array(
'label' => 'Email address:',
'required' => true
));
由于我设置为true,因此确保它不为空。默认错误消息如下所示:
"Value is required and can't be empty"
我尝试在验证器上设置消息,但这会引发致命错误:
$validator = $this->getElement('email')->getValidator('NotEmpty');
$validator->setMessage('Please enter your email address.');
在非对象
上调用成员函数setMessage()
如何将邮件设置为自定义错误消息?
答案 0 :(得分:7)
您需要覆盖/指定NotEmpty-validator,而不是使用默认值:
$this->addElement('text', 'email', array(
'label' => 'Email address:',
'required' => true,
'validators' => array (
'NotEmpty' => array (
'validator' => 'NotEmpty',
'options' => array (
'messages' => 'YOUR CUSTOM ERROR MESSAGE'
)
)
)
));
答案 1 :(得分:1)
$neValidator = new Zend_Validate_NotEmpty();
$neValidator->setMessage('Please enter your email address.');
$textElement = new Zend_Form_Element_Text('email');
$textElement->addValidator($neValidator);