A free and open-source book on ZF3 for beginners


7.15. Summary

Forms are the way of collecting user-entered data on web pages. A form usually consists of elements (input field + label pairs). Elements can optionally be grouped into fieldsets.

In an MVC-based web site, form functionality is separated into form models responsible for element definition and validation, and form presentation implemented with the help of special view helpers.

To create a form model, you write a class deriving from the Form base class. The form model is initialized by adding its elements with the help of the base class-provided methods.

To submit form data to the server, the user clicks the Submit button, then the data is sent as part of a HTTP request. Once the user submits the form, you can extract the form data in your controller and ask the form model to validate it.

For checking and filtering the user-entered data, filters and validators are utilized. You use the InputFilter class which is the container for validation rules.

If there are input errors, you display the form again, asking the user to correct the input errors. If the data is correct, you process the data with your business logic layer.


Top