Configuring Zend Framework 2 Email Field Validation Error Messages

I have an input filter whose authentication template for the email field is as follows:

'validators' => array( array ( 'name' => 'EmailAddress', 'options' => array( 'messages' => array( 'emailAddressInvalidFormat' => "Email address doesn't appear to be valid.", ) ), ), array ( 'name' => 'NotEmpty', 'options' => array( 'messages' => array( 'isEmpty' => 'Email address is required', ) ), ), ), ), 

It works, this part is fine, but what I will laugh forever from the business units here is if I post an application that spits out this error message to users:

The input does not match the pattern

'/ ^ [A-Za-Z0-9 # $% &.!? + / = ^ _ `{|} ~ -] + @ [A-Za-Z0-9 -] + (?.: [A-Za-Z0-9 -] +) $ / '

There a strange bearded comedy buried there (yes, I understand that for sure, but, rofl).

I have two questions for good souls here:

How can I customize this error message? I cannot find the correct key, as easy for 'emailAddressInvalidFormat' .

Also, is it possible to flip all errors into one? What I mean? Instead of posting:

"Your email template has just left the building, and your email address cannot be blank and your email address is not displayed.

Is it possible to send a “single refusal” email?

"Hey bud, check your email, something is wrong!"

Thanks for your help, as always.

UPDATE

Vote for this error here https://github.com/zendframework/zend-validator/issues/41

+6
source share
4 answers

Try this for a special email to check email in ZF2:

  'validators' => array( array( 'name' => 'EmailAddress', 'options' => array( 'messages' => array( \Zend\Validator\EmailAddress::INVALID_FORMAT => '"Hey bud, check your email, something ain\'t right!"' ) ) ) ) 
+4
source

According to ZF2 email address validation:

  $this->add(array( 'name' => 'email', 'required' => true, 'validators' => array( array( 'name' => 'EmailAddress', 'options' => array( 'message' => array( \Zend\Validator\EmailAddress::INVALID => "Invalid type given. String expected", \Zend\Validator\EmailAddress::INVALID_FORMAT =>"The input is not a valid email address. Use the basic format local-part@hostname ", \Zend\Validator\EmailAddress::INVALID_HOSTNAME =>"'%hostname%' is not a valid hostname for the email address", \Zend\Validator\EmailAddress::INVALID_MX_RECORD =>"'%hostname%' does not appear to have any valid MX or A records for the email address" , \Zend\Validator\EmailAddress::INVALID_SEGMENT =>"'%hostname%' is not in a routable network segment. The email address should not be resolved from public network", \Zend\Validator\EmailAddress::DOT_ATOM =>"'%localPart%' can not be matched against dot-atom format" , \Zend\Validator\EmailAddress::QUOTED_STRING =>"'%localPart%' can not be matched against quoted-string format", \Zend\Validator\EmailAddress::INVALID_LOCAL_PART =>"'%localPart%' is not a valid local part for the email address" , \Zend\Validator\EmailAddress::LENGTH_EXCEEDED =>"The input exceeds the allowed length", ), ), ), ), )); 

And an additional check could be Regex , as mentioned above, and I also have NoObjectExists to make sure the email is not in my db if the email address is used to login:

 array( 'name' => 'DoctrineModule\Validator\NoObjectExists', 'options' => array( 'object_repository' => $sm->get('doctrine.entitymanager.orm_default')->getRepository('MyMudole\Entity\User'), 'fields' => 'email', 'message'=>'This email is associated with another user! Please use another email', ), ), 
+2
source

"Input does not match the pattern" seems to be Zend\Validator\Regex::NOT_MATCH , not the Zend\Validator\EmailAddress class.

It is not clear from your code where and whether you use the Regex validator. I believe EmailAddress does not use Regex internally.

If you want to customize a Regex post, it will probably look like this:

 array ( 'name' => 'Regex', 'options' => array( 'messages' => array( 'regexNotMatch' => "Not so nerdy message here.", ) ), ), 
0
source

It is not possible to specify only one message, but it should work as well:

  $message = sprintf( $this->getTranslate()->translate( '%s filled is not a valid e-mail address, please inform a valid in the field.' ), $entity ); return array( 'name' => 'EmailAddress', 'options' => array( 'messages' => array( \Zend\Validator\EmailAddress::INVALID => $message, \Zend\Validator\EmailAddress::INVALID_FORMAT => $message, \Zend\Validator\EmailAddress::INVALID_HOSTNAME => $message, \Zend\Validator\EmailAddress::INVALID_MX_RECORD => $message, \Zend\Validator\EmailAddress::INVALID_SEGMENT => $message, \Zend\Validator\EmailAddress::DOT_ATOM => $message, \Zend\Validator\EmailAddress::QUOTED_STRING => $message, \Zend\Validator\EmailAddress::INVALID_LOCAL_PART => $message, \Zend\Validator\EmailAddress::LENGTH_EXCEEDED => $message, ), ), 'break_chain_on_failure' => true ); 
0
source

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


All Articles