How to return "isEmpty" error message from Zend \ Validator \ Identical in Zend Framework 2?

Possible duplicate:
zendframework 2 inputfilter set default error message

I am trying to use Zend \ InputFilter \ InputFilter to validate input from a registration form. I have the code below:

  • Confirms the email address in the "email" field; then
  • Checks that the value in 'email_confirm' matches the value in 'email'.

This works in all cases, except when the user leaves both fields blank. In this case, the validator for 'email_confirm' returns an error Array ( [isEmpty] => Value is required and can't be empty ) .

How to configure this error message? I can not install it using:

 'messages' => array( 'isEmpty' => 'Message Here' ), 

because it throws an exception (quite rightly) that Zend \ Validator \ Identical does not have a message template for 'isEmpty'. And it does not pick up the messages that I previously set for the "email" field, otherwise it will return 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", ), ), ), ), ))); 

Thanks Neil

+4
source share
1 answer

You should use the constant Zend\Validator\NotEmpty::IS_EMPTY as follows:

 array( 'name' =>'NotEmpty', 'options' => array( 'messages' => array( \Zend\Validator\NotEmpty::IS_EMPTY => 'Please enter your email address' ), ), ), 

You can check the API for more information.

EDIT

Diving into the source code Zend \ Validator \ NotEmpty I see that the constant IS_EMPTY has the value 'isEmpty' , so it will not solve your problem. In any case, you should use these constants instead of direct values ​​to avoid possible problems with updating.

0
source

Source: https://habr.com/ru/post/1445498/