Zend_Form is a powerful component of Zend Framework which can hardly be overrated. When trying to work with it for the first time it can seem to a developer that Zend_Form is complicated and unwieldy. It requires writing much more code in comparison with creating a form in a usual way. But after a bit of practice with Zend_Form every programmer will understand that it is quite the cheese. There is no need to write some code manually, Zend_Form will do it for you. The developer just needs to get the proper data and to work with them. And Zend economizes your time again and again using its “tasty” features.
The main advantages of Zend_Form are:
1. Validation and filtration of data inserted via form elements, plus errors output feature.
2. Elements and forms visualization, including screening.
3. Elements and forms grouping.
4. Forms and elements configuration.
It is possible to create all the form elements with Zend_Form. Below you can find more detailed description of the advantages:
1. The biggest plus of Zend_Form is validators availability. They allow to escape the routine of input data checking and to get the necessary data array. As a result, it needs much less time to work with Zend_Form than to write code manually. The created class can be modified with adding or removing some elements and/or validators. If there is a lack of basic validators, developer can create his own one and add it to the system. Hereunder you can find an example of Select element creation and validation settings for advanced Zend_Form class demonstration. While creating an element there is a possibility to define Options tag with an associative array. The key is Value and the array’s element parameter is Text. The validator that checks input data is created automatically. If any input data does not correspond with some of array’s keys, the element will notify the error. By default the error is shown next to the element. There is no need to do something except choosing
the associated array. That’s all, the rest Zend does for you. It means that working with the component you do not have to reinvent the wheel, you just need to adjust the settings correctly. Error notifications can be multilingual, and the choice of fitting language is made automatically. Zend_Form also gives an opportunity to use filters and to transform input data. The basic edition of Zend framework includes enough filters but in case of necessity it is possible to write your own filter.
2. Every object created with Zend_Form can be outputted entirely or in parts depending on the ease of use. It is pretty good that there is no need to write HTML code for elements creation. It is enough to make the output of the entire form or of its element, and the necessary HTML file is generated without any effort. If you are not satisfied with a standard element view, it may be flexibly adjusted by View help.
3.Zend_Form allows to batch elements in groups for easier visual perception. It is very convenient that you do not need to group elements manually. You just should choose a grouping method pointing elements that are to be grouped.
4. Every element of Zend_Form and the whole form as well can be easily adjusted. It is possible to add attributes and properties for any element, to enrich the elements with decorators (tags), to change the element position etc.
To assign parameters for different form elements it is enough to use Populate method. The necessary data are passed by associative array. The key of the form elements is its name, and the element index is shown by the key’s index. Please try this scheme, you may find it suitable for your needs.
The validation method is excellently implemented. Firstly an object of the form should be chosen and then the validation method is called. GET or POST parameters should be passed to the method. It will return True or False result. If the elements pass the validation successfully, choose the method that returns validated and filtered data to work with further. If the data are not validated, just show the form, and the errors will be presented by Zend.
In Zend_Form we can check the data with AJAX requests. There are methods that allow to verify AJAX data and to show JSON.
In conclusion it should be mentioned that Zend_Form use can make an application faster, safer and generally better. So it is time to let Zend code instead of you.