Reputation: 13527
I have the following validation setup for an email field:
$inputFilter->add($factory->createInput(array(
'name' => 'email',
'required' => true,
'filters' => array(
array('name' => 'StripTags'),
array('name' => 'StringTrim'),
),
'validators' => array(
array(
'name' => 'StringLength',
'options' => array(
'encoding' => 'UTF-8',
'min' => 1,
'max' => 255,
),
),
array(
'name' => 'EmailAddress',
'options' => array(
'messages' => array(
\Zend\Validator\EmailAddress::INVALID_HOSTNAME => 'You did not enter a valid e-mail address.'
),
),
)
),
)));
The problem is that if I type in something like: "bla@bla", I get the following triggered errors:
You did not enter a valid e-mail address.
The input does not match the expected structure for a DNS hostname
The input appears to be a local network name but local network names are not allowed
It's way too tedious to do this:
array(
'name' => 'EmailAddress',
'options' => array(
'messages' => array(
\Zend\Validator\EmailAddress::INVALID => 'You did not enter a valid e-mail address.' ,
\Zend\Validator\EmailAddress::INVALID_FORMAT => 'You did not enter a valid e-mail address.' ,
\Zend\Validator\EmailAddress::INVALID_HOSTNAME => 'You did not enter a valid e-mail address.' ,
\Zend\Validator\EmailAddress::INVALID_LOCAL_PART => 'You did not enter a valid e-mail address.' ,
\Zend\Validator\EmailAddress::INVALID_MX_RECORD => 'You did not enter a valid e-mail address.' ,
\Zend\Validator\EmailAddress::INVALID_SEGMENT => 'You did not enter a valid e-mail address.' ,
),
),
)
Is there no better way to do this?
UPDATE
Also, this doesn't work. I still end up with:
You did not enter a valid e-mail address.
The input does not match the expected structure for a DNS hostname
The input appears to be a local network name but local network names are not allowed
Any thoughts?
Upvotes: 1
Views: 593
Reputation: 1
The solution to the problem is as follows:
'message' => array(
\Zend\Validator\EmailAddress::INVALID_HOSTNAME=>'Email WRONG!',
),
Upvotes: 0
Reputation: 15053
Calling setMessage
on the validator without a second parameter will set all the messages:
$email = $factory->createInput(array(
'name' => 'email',
'required' => true,
'filters' => array(
array('name' => 'StripTags'),
array('name' => 'StringTrim'),
),
'validators' => array(
array(
'name' => 'StringLength',
'options' => array(
'encoding' => 'UTF-8',
'min' => 1,
'max' => 255,
),
)
),
));
$emailValidate = new Zend_Validate_EmailAddress();
$emailValidate->setMessage('You did not enter a valid e-mail address.');
$email->addValidator($emailValidate, TRUE);
$inputFilter->add($email);
Not sure if the following also works, but you could give it a try:
array(
'name' => 'EmailAddress',
'options' => array(
'message' => 'You did not enter a valid e-mail address.'
),
)
Upvotes: 1