Extending Zend Form Element to create customized Phone number field

      Comments Off on Extending Zend Form Element to create customized Phone number field

Anis Ahmed has created a new class he has created to help Zend Framework programmers deal with displaying and validating phone numbers. He details his new component in his blog post “Extending Zend Form Element to create customized Phone number field

Generally, Phone numbers has different parts. Such as, country code, operator code and subscriber number etc. When taking Phone number as user input, we can worn users about phone number format by setting a hint/description and can validate using Regular Expression. But, for better usability, along with hints, some web applications provide separate (generally 3) text fields for different parts of phone number. Also, some of them keep a separator, ‘-’ or ‘#’ between this fields.

Now, if we try provide this feature in Zend Form, that’s possible. We can create three individual Zend_Form_Element_Text objects and join their value together to make the phone number. But, in this case, validating them together is a hassle. Again, placing them in line in the form and putting separators needs some Decorator magic. What if we can make it simply another Zend Form Element which renders three text box (optionally with separator)? And consider their value jointly and validate them together using ordinary Zend Validators? Lets try.

The blog post is short and there is a good amount of code in it. If you are currently dealing with this problem, the code is “cut and paste ready”. If you don’t currently deal with phone numbers, trust me, you will at some point, bookmark the post so you will be able to find it when you need it.