I want to validate that zip codes are valid for users. I am using the Zend Framework 2. The form field type is 'text' (I have tried value options but it does not seem to work for the text element). Is there a way I can validate this on the form or in the model (input filter)? I have a list of zip codes in the database so I can use an inArray check in the controller but would like to include it in the model or form if possible. Thanks
ZF2 validates the given postal code based on predefined REGEX patterns (for US region they use following REGEX pattern '\d{5}([ -]\d{4})?').
So, ZF2 always returns TRUE, if you pass 11111.
Thanks
may be you can write a class in Form folder extend Zend\Validator\AbstractValidator (exmple: zipcodeCheck) and you must implements the function isValid(), you can create some REGEX statement such like :
public function isValid($value){
if(!preg_match('\d{5}([ -]\d{4})?', $value)){// $value is the data you want to validate
return false;
}
return true;
}
and your Filter must add:
$this->add(array(
'name' => 'zipcode',
'validators' => array(
array(
'name' => '\Mmc\Form\yourClassName',//zipcodeCheck may be
'options' => array(
'error_title' => 'Zipcode From',
)
)
),
));