我为电子邮件字段设置了以下验证设置:
$inputFilter->add($factory->createInput(array(
'name' => 'email',
'required' => true,
'filters' => array(
array('name' => 'StripTags'),
array('name' => 'StringTrim'),
),
'validators' => array(
array(
'name' => 'StringLength',
'options' => array(
'encoding' => 'UTF-8',
'min' => 1,
'max' => 255,
),
),
array(
'name' => 'EmailAddress',
'options' => array(
'messages' => array(
\Zend\Validator\EmailAddress::INVALID_HOSTNAME => 'You did not enter a valid e-mail address.'
),
),
)
),
)));
问题是,如果我输入类似“bla @ bla”的内容,我会收到以下触发错误:
You did not enter a valid e-mail address.
The input does not match the expected structure for a DNS hostname
The input appears to be a local network name but local network names are not allowed
这样做太繁琐了:
array(
'name' => 'EmailAddress',
'options' => array(
'messages' => array(
\Zend\Validator\EmailAddress::INVALID => 'You did not enter a valid e-mail address.' ,
\Zend\Validator\EmailAddress::INVALID_FORMAT => 'You did not enter a valid e-mail address.' ,
\Zend\Validator\EmailAddress::INVALID_HOSTNAME => 'You did not enter a valid e-mail address.' ,
\Zend\Validator\EmailAddress::INVALID_LOCAL_PART => 'You did not enter a valid e-mail address.' ,
\Zend\Validator\EmailAddress::INVALID_MX_RECORD => 'You did not enter a valid e-mail address.' ,
\Zend\Validator\EmailAddress::INVALID_SEGMENT => 'You did not enter a valid e-mail address.' ,
),
),
)
有没有更好的方法呢?
更新
此外,这不起作用。我仍然最终:
You did not enter a valid e-mail address.
The input does not match the expected structure for a DNS hostname
The input appears to be a local network name but local network names are not allowed
有什么想法吗?
答案 0 :(得分:1)
在没有第二个参数的情况下在验证器上调用setMessage
将设置所有消息:
$email = $factory->createInput(array(
'name' => 'email',
'required' => true,
'filters' => array(
array('name' => 'StripTags'),
array('name' => 'StringTrim'),
),
'validators' => array(
array(
'name' => 'StringLength',
'options' => array(
'encoding' => 'UTF-8',
'min' => 1,
'max' => 255,
),
)
),
));
$emailValidate = new Zend_Validate_EmailAddress();
$emailValidate->setMessage('You did not enter a valid e-mail address.');
$email->addValidator($emailValidate, TRUE);
$inputFilter->add($email);
不确定以下内容是否也有效,但您可以尝试一下:
array(
'name' => 'EmailAddress',
'options' => array(
'message' => 'You did not enter a valid e-mail address.'
),
)
答案 1 :(得分:0)
问题的解决方案如下:
'message' => array(
\Zend\Validator\EmailAddress::INVALID_HOSTNAME=>'Email WRONG!',
),