Skip Navigation LinksHome > Categories > Code from a Category

Forms in Zend Framework

User Name: serfcompany
Name: Serf
Contact Me:
Home Page:
php,mysql,javascript,html,css. Preferable working with Zend Framework. Good know javascript. I worked with various, javascript frameworks such as(jquery, YUI3, extjs, sencha touch). [More]
Viewed Times: 2764
Add Date: 08/08/2012
In this article we consider how to create forms in Zend Framework.

In the paradigm of the model-view-controller form is always difficult playing a role. Of course, the form - it's just HTML, but for me it is something more abstract.

In fact, the form - it is HTML, which means the user enters and retrieves data, but other than that the form still performs normalization, validation, data filtering, and error messages, if any.
This may require quite a significant amount of code.

If you can automate all of these things, then you will only configuration.
Each form consists of elements, each element has several attributes that override the functionality of the form.
Let's look at some of these aspects.

Create a form.

Form - an instance of Zend_Form. A copy can be created in many ways.

I prefer to create their own class, inheriting class Zend_Form. And in my classes I am submitting the necessary changes.

Each form consists of several important fields (action, method) and contains at least one element. Let's see how you can create a.
Each element can have several types of attributes: the type of field, label, name and description.

Let's define an element in ZF. Please note there are several different ways to create forms. This is my favorite way.

As you can see in the example I'm asking all of these attributes.

NB. translator:
field type - Zend_Form_Element_Text
label (label) - 'Username'
name - 'username'
description (description) - 'This is a description'

In addition to these there are a few important attributes of the element. Let's add some more.

Filters and validators

Building chains of filters and validators - this is one of the very useful features of the component shapes.
Filter - this is such a thing, which automatically performs the conversion of the input data. A validator - a mechanism for automatic data validation when processing forms. These mechanisms greatly simplify the code and the controller model, as the logic is moved to a more organized structure.

Consider the following example. On the registration form is an element of a user name. It is necessary to realize the uniqueness of the test name, the name should contain only permitted characters, and its length must be in the acceptable range.

If you have an element on the form in which the user can enter any text, such as yourself, then you'll want to apply filters to make sure that introduced "clean" data.

It will be useful to add a filter type StripTags, to remove all the tags HTML, StringTrim, to remove any extra spaces. If you wish, you can add filters to handle the BB-codes. To do this, you can create your own filter or use the Callback-filter, which can be used in other forms.

The idea is to check the data as closely as possible, you should be sure that the data is 100% fit your application. If you have a field of type VARCHAR (255), restrict the entry field to 255 characters, and you do not have to use the functions of the cutoff line. Exercise extreme caution.

Filters help to preserve the integrity of the data can also be used in the cleaning process the data entered on the form. If you allow the user to type something, be sure to remove all the tags HTML. I think you get the idea ...

I present a more complete example of the registration form:

Skinny controller, fat model

For the form described above, you can develop a very simple controller. I think that the controller has to manage application logic, but it should not work with the data. Here is an example of action (action) for a registration form.

After the user completes the data entry, validators perform validation of the entered information. If the test succeeds, the data array is passed to the model. By the time the data into the model all the filters and validators must finish its work. Now we already know how to format the data and how to improve their security. In the controller you can redirect the user to another page, or perform other actions in accordance with the logic of your application.

The form is displayed to the user through a view. In general, the view simply loads the form and displays it to the user. If the user data contains an error, the view is not only displays the form and displays error messages, but also shows the input data.

Script submission and decorators

Below is all the code view, with which you can display the form. If you need additional configuration form, it can be done by means of CSS or decorators.

By default, Zend_Form generates quite a decent code of HTML. I do not have any problems with the imposition of additional styles CSS. Using CSS fits into the concept of separation of different functional parts of the application. However, you often need to change not only the display style, and layout. The Zend for this purpose decorators. Decorators more difficult to understand, so I'll leave it to the next post.

The interaction with the models.

In accordance with the code in our controller, the model result of the function passed to $ form-> getValues ??(). The result of this function - pre-tested and the filtered data array.
Here's an example:

I think it's fine. We work with a very simple interface, and we do not need to manually specify every value that we want to pass. Of course, this master's work, but I do not understand people who send in the model values ??of 5-10. These controllers will certainly be large.
Of course, the code does not depend on the form, because it works with an array of data. This makes the challenge model is very simple. Model - a place where you want to start a complex code.
With this approach, we can use our adapter object database or some other libraries.


The component form (form) - this is one of the biggest, and perhaps the biggest component in Zend. Do not be afraid to ask questions in various forums, or ask questions in the form below, I will direct you to a good resource. As I said, for me it is the most useful component in Zend. I strongly recommend not to spare the time to study it.

Post a Comment

Name: (Optional)
Email: (Optional, you can get an email if somebody replys your comments)*
Email me if somebody respons my comment below:
Enter Text
as Below:
(case insensitive, if hard to read, click the "get a new one" button)
* Your email address will not be shared with any third parties for any reason.
** Maximum 1000 charactors.