A free and open-source book on ZF3 for beginners


7.12. Form Presentation

When your controller's action is ready, all you have to do is prepare the .phtml view template file to display your form on a web page. In the view template, you need to define the markup using <form>, <label>, <input>, and possibly other HTML tags.

Additionally, you will have to display error messages if the form validation failed. Because this work is rather boring, Zend Framework 3 provides you with special view helpers intended for rendering the form.

For simple forms (which do not show error messages), you can use raw HTML tags for rendering the form and ignore ZF3-provided form view helpers. But, form view helpers are really unavoidable when rendering complex forms that may display validation errors and/or add fields dynamically.

7.12.1. Preparing the Form Model for Rendering

Before rendering, it is required that you call the prepare() method on the form model's instance (see table 7.12). If you forget to call this method, there may be undesired effects.

Table 7.12. Methods provided by the Form base class
Method name Description
prepare() Ensures the form state is ready for use.

The prepare() method does the following form model preparations:

25) Typically, this results in wrapping field names with the form/fieldset name (for example, the "email" field's name will become "contact-form[email]") which technically results in a more convenient field grouping in a HTTP request body.


Top