I am often asked what my favorite component is in the Zend Framework, and I always answer: “Forms” (forms).
In the paradigm, model-representation-controller forms always play a difficult role. Of course, the form is just HTML,
but for me it is something more abstract.
In essence, a form is HTML, by means of which the user enters and receives data, but besides this the form also performs
Normalization, validation, data filtering and error messages, if any.
This may require quite a lot of code.
If you can automate all these things, then you will only have the configuration.
Each form consists of elements, each element has several attributes that override the functionality
forms.
Let's look at some of these aspects.
Form creation')
A form is an instance of the class Zend_Form. An instance can be created in many ways.
I prefer to create my own classes by inheriting the Zend_Form class. And already in my classes I make the changes I need.
Each form consists of several important fields (action, method) and contains at least one element.
Let's see how you can create an item.
Each element can have several type attributes: field type, label, name and description.
Let's describe such an element in ZF.
Please note there are several different ways to create forms. This is my favorite way.
$username = new Zend_Form_Element_Text( 'username' , array(<br> 'label' => 'Username' ,<br> 'description' => 'This is a description' )); <br><br> * This source code was highlighted with Source Code Highlighter .
As you can see, in the example I set all these attributes.
Note translator:
field type - Zend_Form_Element_Text
label (label) - 'Username'
name - 'username'
description (description) - 'This is a description'
In addition to the above, there are several important attributes of the element. Let's add some more.
$ this ->addElements(array(<br> new Zend_Form_Element_Text( 'username' , array(<br> 'label' => 'Username' ,<br> 'required' => true )),<br> )); <br><br> * This source code was highlighted with Source Code Highlighter .
Filters and validatorsBuilding up chains of filters and validators is one of the very useful features of the form component.
A filter is one that automatically converts input data.
A validator is a mechanism for automatic data verification during form processing.
These mechanisms greatly simplify the controller code and model, since logic moves to a more organized
structure.
Consider an example. On the registration form is the input element of the user name. It is necessary to implement the check
uniqueness of the name, the name must consist only of allowed characters and its length must be within the allowable range.
If you have an element on a form in which the user can enter arbitrary text, for example about himself, then you will surely
want to apply filters to make sure that “clean” data is entered.
It will be useful to add filters like StripTags to remove all HTML tags, StringTrim, to remove all extra spaces.
If you wish, you can add filters to handle BB codes.
To do this, you can create your own filter or use a Callback filter, which you can use in other forms.
The idea is to check the data as carefully as possible. You must be sure that the data is 100% consistent with your
application. If you have a VARCHAR type field (255), then limit the input field to 255 characters, and you will not have to use the function to cut lines.
Take great care.
Filters help to preserve the integrity of data, can also be used in the process of cleaning data entered into the form.
If you allow the user to enter something, be sure to remove all HTML tags. I think you understand the idea ...
I present a more complete example of the registration form:
$ this ->addElements(array(<br> new Zend_Form_Element_Text( 'username' , array(<br> 'label' => 'Username' ,<br> 'required' => true ,<br> 'validators' => array(<br> array( 'StringLength' , false , array(4, 16)),<br> array( 'Alnum' ),<br> array( 'Db_NoRecordExists' , false , array( 'users' , 'username' ))<br> )<br> )),<br> new Zend_Form_Element_Text( 'email' , array(<br> 'label' => 'Email Address' ,<br> 'required' => true ,<br> 'validators' => array(<br> array( 'EmailAddress' ),<br> array( 'Db_NoRecordExists' , false , array( 'users' , 'email' ))<br> )<br> )),<br> new Zend_Form_Element_Password( 'password' , array(<br> 'label' => 'Password' ,<br> 'required' => true <br> ))<br>)); <br><br> * This source code was highlighted with Source Code Highlighter .
Slim controller, fat modelFor the form described above, you can design a very simple controller.
I believe that the controller should manage the application logic, but should not work with the data.
Here is an example of the action for the registration form.
if ($ this ->_request->isPost()) <br>{<br> if ($form->isValid($ this ->_request->getPost())) <br> {<br> $user->register($form->getValues());<br> $ this ->redirector->goToUrl( '/welcome' );<br> }<br>}<br>$ this ->view->form = $form; <br><br> * This source code was highlighted with Source Code Highlighter .
After the user completes the data entry, validators validate the information entered.
If the check succeeds, the data array is transferred to the model. By the time data is transferred to the model, all
filters and validators must complete their work. Now we already know how to format the data and how to increase 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 the view. In general, a view simply loads the form and displays it to the user.
If the user input contains an error, the view will not only display the form and display error messages, but
and will show the entered data.
View Scripts and DecoratorsBelow is the entire presentation code with which you can display a form. If additional form adjustment is required, it can be
run by CSS or decorators.
<?= $ this ->form ?> <br><br> * This source code was highlighted with Source Code Highlighter .
By default, Zend_Form generates quite decent HTML code. I do not have any difficulties with the imposition of additional
CSS styles. Using CSS fits well with the concept of separating different functional parts of an application.
However, it is often necessary to change not only the display style, but also the arrangement of elements. Zend uses for this
decorators. Decorators are somewhat difficult to understand, so I will leave them for the next post.
Interaction with modelsIn accordance with the code of our controller, the result of the $ form-> getValues () function is passed to the model.
The result of this function is a previously checked and filtered data array.
Here is an example:
// ... <br> public function register(array $user) {<br><br>}<br>// ... <br><br> * This source code was highlighted with Source Code Highlighter .
I think that's great. We work with a very simple interface, and we do not need to manually specify each value,
which we want to convey. Of course, this is an economic matter, but I do not understand people who transfer 5-10 values to a model.
Such controllers will certainly be large.
Of course, the code does not depend on the form, since it works with an array of data. This makes
calling the model very simple.
The model is the place where the complex code should begin.
With this approach, we can use our database adapter object or several other libraries.
ConclusionThe form component is one of the largest, and perhaps the largest, 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 this is the most
useful component in Zend. I highly recommend taking the time to study it.
You may be interested in the
form caching material.
Petrelevich Sergeypetrelevich@yandex.ruwww.SmartyIT.ru