I’m developing a registration page using Zend Framework.
Phone, Mobile, ID Number are the fields I’d like to Validate. So far I have a validation only for Not Empty.
I couldn’t find the right solution so maybe you can help me out.
I want the phone and mobile fields to be only integers for ex. 12345667 and to have specific length, for ex. minimum 7 digits.
and the same thing goes with ID NUMBER field too.
This is how it looks right now:
$phone = new Zend_Form_Element_Text('phone');
$phone->setLabel('Phone')
->setAttribs(array('class' => 'inputtext'))
->setOptions(array('size' => '50'))
->setRequired(false)
->addFilter('StripTags')
->addFilter('StringTrim')
->addValidator('NotEmpty');
Let’s try and give you some answer for your question.
If you don’t already realize it the validators in Zend_Form are the same validators used as standard validators in Zend_Validate, you usually just use the class name as a string instead of calling new. The same is true for filters and Zend_Filter
The closest standard validator available for a phone number would be the ‘Digits’ validator.
However you may find it more appropriate to construct your own validator by extending Zend_Validate_Abstract. A phone number validator might look like:
and would be used in your form element:
One thing to keep in mind: If you set a form element as
'Required'using the'NotEmpty'validator is redundant in most cases assetRequired()calls'NotEmpty'internally.Also remember that typically in Zend_Form filters are applied prior to validation.
Hope this helps.