我有下一个元素:
$email = new Zend_Form_Element_Text('email');
$email->setAttribs(array('class' => 'input-text', 'id' => 'email'))
->setLabel($this->view->translate('Email'))
->setValue(null)
->setRequired(true)
->addValidator(new Zend_Validate_EmailAddress())
->setDecorators($emailMessageDecorators);
如果电子邮件地址中存在多个错误,则会显示一些错误。像这样:
'fff.fgdf' is no valid hostname for email address 'as@fff.fgdf'
'fff.fgdf' appears to be a DNS hostname but cannot match TLD against known list
'fff.fgdf' appears to be a local network name but local network names are not allowed
如何只设置1条消息?我尝试过setMessage(string),但它显示了3个相同的错误。谢谢。对不起我的英语不好。和平与安宁爱)
答案 0 :(得分:2)
如果我没记错的话,可以调用setErrorMessages()在表单元素上设置单个错误消息,而不是在每个单独的验证器上调用setMessages():
$form->addElement('password', 'password', array(
'label' => 'New password:',
'required' => true,
'validators' => array(
array('StringLength', false, 6),
// more validators
),
'errorMessages' => array('Invalid password.')
));
答案 1 :(得分:2)
过去,我必须为此制作一个自定义验证器:
/**
* The standard email address validator with a single, simple message
*/
class App_Validate_EmailAddressSimpleMessage extends Zend_Validate_EmailAddress
{
const COMMON_MESSAGE = 'Invalid email address';
protected $_messageTemplates = array(
self::INVALID => self::COMMON_MESSAGE,
self::INVALID_FORMAT => self::COMMON_MESSAGE,
self::INVALID_HOSTNAME => self::COMMON_MESSAGE,
self::INVALID_MX_RECORD => self::COMMON_MESSAGE,
self::INVALID_SEGMENT => self::COMMON_MESSAGE,
self::DOT_ATOM => self::COMMON_MESSAGE,
self::QUOTED_STRING => self::COMMON_MESSAGE,
self::INVALID_LOCAL_PART => self::COMMON_MESSAGE,
self::LENGTH_EXCEEDED => self::COMMON_MESSAGE,
);
}
然后使用:
调用 $email->addValidator(new App_Validate_EmailAddressSimpleMessage());
如果您只想使用与往常相同的语法:
$email->addValidator('EmailAddress');
但让它使用此验证器,然后您可以将类名/文件名从EmailAddressSimpleMessage
更改为简单EmailAddress
,并使用表单/元素注册前缀App_Validate_
。
更好的做法可能是允许这个类接受你想要的消息的可选构造函数参数,但当时我的速度很快。
答案 2 :(得分:1)
这是一个例子(我希望它有所帮助):
$email = new Zend_Form_Element_Text('emailid');
$email->setLabel("Email-Adress :* ")
->setOptions(array('size' => 20))
->setRequired(true)
->addFilter('StripTags')
->addFilter('StringTrim')
->addValidator('EmailAddress')
->getValidator('EmailAddress')->setMessage("Please enter a valid e-mail address.");
答案 3 :(得分:0)
这是答案,使用$ breakChainOnFailure = true:
addValidator($nameOrValidator, $breakChainOnFailure = false, array $options = null);
答案 4 :(得分:-1)
您使用此示例:
$email_validator = new Zend_Validate_EmailAddress();
$email = new Zend_Form_Element_Text('email');
$email->setRequired('true')
->setLabel('Email: ')
->setDecorators(array(array('ViewHelper'), array('Errors')))
->addValidator($email_validator);