我正在尝试使用 Zend\InputFilter\InputFilter 来验证来自注册表单的输入。我有下面的代码:
- 验证“电子邮件”字段中的电子邮件地址;然后
- 检查“email_confirm”中的值是否与“email”中的值匹配。
这适用于所有情况,除了用户将两个字段都留空时。在那种情况下,'email_confirm' 的验证器会返回错误Array ( [isEmpty] => Value is required and can't be empty )
。
如何自定义此错误消息?我无法使用以下方式设置它:
'messages' => array(
'isEmpty' => 'Message Here'
),
因为这会引发一个异常,说(非常正确) Zend\Validator\Identical 没有“isEmpty”的消息模板。而且它不会接收我之前为“电子邮件”字段设置的消息,否则它会返回Array ( [isEmpty] => Please enter your email address )
.
$this->add($inputFactory->createInput(array(
'name' => 'email',
'required' => true,
'filters' => array(
array('name' => 'StripTags'),
array('name' => 'StringTrim'),
),
'validators' => array(
array(
'name' =>'NotEmpty',
'options' => array(
'messages' => array(
'isEmpty' => 'Please enter your email address'
),
),
),
array(
'name' => 'StringLength',
'options' => array(
'encoding' => 'UTF-8',
'max' => 200,
'messages' => array(
'stringLengthTooLong' => "Email addresses cannot be more than 200 characters"
),
),
),
array(
'name' =>'EmailAddress',
'options' => array(
'useMxCheck' => true,
),
),
),
)));
$this->add($inputFactory->createInput(array(
'name' => 'email_confirm',
'required' => true,
'filters' => array(
array('name' => 'StripTags'),
array('name' => 'StringTrim'),
),
'validators' => array(
array(
'name' => 'Identical',
'options' => array(
'token' => 'email',
'messages' => array(
'notSame' => "Your email addresses do not match, please try again",
),
),
),
),
)));
谢谢,尼尔