Saeven
Saeven

Reputation: 2300

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

Upvotes: 7

Views: 5704

Answers (4)

Oskar
Oskar

Reputation: 2562

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',
    ),
),

Upvotes: 2

It's not possible to especify just 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
    );

Upvotes: 0

D Singh
D Singh

Reputation: 295

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!"' 
                        )             
                    )                   
                )             
            )         

Upvotes: 4

Dennis Gawrisch
Dennis Gawrisch

Reputation: 1060

“The input does not match against pattern” message seems to be Zend\Validator\Regex::NOT_MATCH actually, not of Zend\Validator\EmailAddress class.

From your code, it is not clear where and whether you make use of the Regex validator. I believe EmailAddress does not use Regex internally.

If you would like to customize the Regex message, it would probably look like this:

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

Upvotes: 0

Related Questions