Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Customizing Zend Framework 2 Email Field Validator Error Messages

I've got an Input Filter whose validator config for an email field looks like;

'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, that part is fine, but what I will get forever laughed at by the business units here, is if I put out an app that spits this error message to users:

The input does not match against pattern

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

There's a strange nerd comedy buried in there (yes I realize it's accurate, but, rofl).

I have two questions for the kind souls here:

How can I customize that error message? I can't seem to find the right key as I easily had for 'emailAddressInvalidFormat'.

Also, Is it possible to roll all the errors up into one? What I mean by that is. Rather than posting:

"Your email pattern just left the building & Your email cannot be blank & Your email doesn't appear to be valid"

Can I put a "single failure" message for email?

"Hey bud, check your email, something ain't right!"

Thanks for your help as always.

UPDATE

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

like image 369
Saeven Avatar asked Jul 18 '13 13:07

Saeven


2 Answers

Try this for custom message for email validation 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!"' 
                        )             
                    )                   
                )             
            )         
like image 55
D Singh Avatar answered Oct 09 '22 21:10

D Singh


According to ZF2, Email address validations are:

  $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 additional validation could be Regex as stated 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',
    ),
),
like image 40
Oskar Avatar answered Oct 09 '22 20:10

Oskar