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 there value together to make the phone number. But, in this case, validating them together is a hassle. Again, placing them inline 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.
What we are going to make?
Let’s visualize our Phone number field first and list what we want from it.
- It’s Simple. It’s rendering, validation everything is like any other ordinary Zend From Elements.
- It prints 3 text boxes for different parts of Phone number.
- When we validate it or take it’s value, the input of these three boxes jointly will be considered as the value.
- We can set a separator character to be printed between text boxes.
- We can include/exclude this separators from value.
Continue reading →