Reputation: 5248
Am litle confused with email validations. In my model i define rules for form fields. My problem is that I do not know where to put Zend\Validator\EmailAddress()
rule. In standard InputFilter i have standard filters and validators like : require
, min
, max
, encode
ect. And does ZF2 supports validation for phone numbers ?
I read this but i dont know how to mix EmailAddress rules in my model array getInputFilter()
. Do i need define email validations in controller or i can do that in model ?
http://framework.zend.com/manual/2.0/en/modules/zend.validator.set.html
My Model use Zend\InputFilter\ÍnputFilter and i set rule like this :
/**
* Company Rules
*
* @filters
* StripTags | StringTrim
* @validators
* StringLenght | UTF-8 encoding | min | max
*/
$filter->add(array(
'name' => 'company',
'required' => true,
'filters' => array(
array('name' => 'StripTags'),
array('name' => 'StringTrim'),
),
'validators' => array(
array(
'name' => 'StringLenght',
'options' => array(
'encoding' => 'UTF-8',
'min' => 2,
'max' => 100,
),
),
),
));
/**
* Fax Rules
*
* @filters
* StripTags | StringTrim | Int
* @validators
* StringLenght | UTF-8 encoding | min | max
*/
$filter->add(array(
'name' => 'fax',
'required' => true,
'filters' => array(
array('name' => 'StripTags'),
array('name' => 'StringTrim'),
),
'validators' => array(
array(
'name' => 'StringLenght',
'options' => array(
'encoding' => 'UTF-8',
'min' => 2,
'max' => 100,
),
),
),
));
}
}
Upvotes: 1
Views: 5385
Reputation: 561
You can mix different validators writing styles in the following way:
'validators' => array(
array(
'name' => 'StringLength',
'options' => ...
),
new \Zend\Validator\EmailAddress()
),
You can also specify EmailAddress validator using an array-like format:
'validators' => array(
array(
'name' => 'StringLength',
'options' => array(
'encoding' => 'UTF-8',
'min' => 2,
'max' => 100,
),
),
array(
'name' => 'EmailAddress',
),
),
Regarding validation of phone numbers, ZF2 doesn't provide validators ready for use (probably because there are so many differences between international phone numbers format), but using Zend\Validator\Regex you could implement one with not too much work.
Upvotes: 8
Reputation: 1
this is the simple and best suited way of email validation at-least for me
'validators' => array(
array(
'name' => 'EmailAddress',
'options' =>array(
'domain' => 'true',
'hostname' => 'true',
'mx' => 'true',
'deep' => 'true',
'message' => 'Invalid email address',
),
),
Upvotes: 0