Forms in Zend Framework
I am often asked what my favorite component in the Zend Framework, and I always answer: "Forms" (forms).
In the paradigm model-view-controller forms always play a difficult role. Of course, the form is just HTML,
but for me it is something more abstract.
In fact, the form is HTML, by means of which a user enters and receives data, but in addition form still performs
normalization, validation, filtering data and displaying error messages, if any.
This may require a rather significant amount of code.
If You can automate all these things, then You will only need configuration.
Each form consists of elements, each element has several attributes that override functionality
form.
Let's look at some of these aspects.
the Create form
Form is an instance of Zend_Form class. You can create an instance in many ways.
I prefer to create my classes and inherits the Zend_Form class. And in your classes make me change.
Each form consists of several important fields (the action method) and contains at least one element.
Let's see how you can create an item.
Each item may have some typical attributes: a field type, label, name and description.
Let's describe such an element in ZF.
Please note, there are several different ways of creating forms. This is my favorite way.
As You can see in the example I set all these attributes.
Approx. interpreter:
field type — Zend_Form_Element_Text
label (label) — 'Username'
name: 'username'
description (description) — 'This is a description'
In addition to the above there are a few important attributes of the item. Let's add some more.
the Filters and validators
Building chains of filters and validators is one of the very useful features of the component shapes.
The filter is the sort of thing that automatically performs conversion on the input data.
A validator is a mechanism for automatic data validation during form processing.
These mechanisms greatly simplify the controller code and the model, since the logic is moved to a more organized
structure.
Let's consider an example. On the registration form is the input element user name. Need to implement validation
the uniqueness of the name must only consist of permitted characters and its length must enter in a valid range.
If You have on the form is the element in which the user can enter arbitrary text, such as about yourself, then You certainly
want to apply filters to ensure that you have entered the "clean" data.
It would be useful to add filters of the type StripTags to remove all HTML tags, StringTrim, to remove all extra spaces.
If you wish, you can add filters to handle the BB-codes.
For this you can create your own filter or use the Callback filter that can be used in other forms.
The idea was to check the data as thoroughly as possible, You need to be sure that data is 100% consistent with your
Exercise maximum caution.
Filters help maintain the integrity of the data can also be used in the process of cleaning the data entered in the form.
If You allow the user to enter something, be sure to remove all HTML tags. I think You get the idea...
Provide a more complete example of the registration form:
the Skinny controller, fat model
For the form described above, You can develop a very simple controller.
I believe that the controller should manage the application logic, but should not work with the data.
Here is a sample action (action) for the registration form.
After the user finishes the input, the validator checks the entered information.
If the test ends successfully, the array data is passed to the model. By the time of data transmission in all model
filters and validators should finish the job. Now we already know how to format the data and how to improve their security.
In the controller You can redirect user to another page or to perform other actions in accordance with the logic of Your application.
A form is displayed to the user through the view. In the General case, the representation simply loads the form and displays it to the user.
If the user entered data contains an error, the view not only displays the form and display error messages, but
and will show the entered data.
the Scripts views, and decorators
Shown below is the entire code representation, which can be used to display the form. If additional configuration is required form, it can
to run CSS or decorators.
By default, Zend_Form generates quite decent HTML code. I do not experience any difficulties with the imposition of additional
CSS styles. Using CSS fits well with the concept of separation of different functional parts of the application.
However, you often need to change not only the display style and position of elements. Zend for this purpose,
the Interaction models
In accordance with the code of our controller model is passed to the function $form->getValues().
The result of this function is pre-tested and the filtered data array.
Here is an example:
I think that's fine. We work with a very simple interface, and we don't have to manually specify each value,
we want to convey. Of course, it's up to, but I don't understand people who passed in model 5-10 values.
These controllers will certainly be great.
Of course, the code does not depend on the shape, because it works with the array data. It makes the call model is very simple.
Model is the place where you want to start complex code.
With this approach, we can use our adapter object database or some other libraries.
the Conclusion
Comonent form (form) is one of the biggest, and perhaps the largest component in Zend. Don't 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, to me this is
a useful component in Zend. I strongly recommend to invest time to study it.
You may be interested in the material on caching forms.
Petrulevich Sergei
petrelevich@yandex.ru
www.SmartyIT.ru
Article based on information from habrahabr.ru
In the paradigm model-view-controller forms always play a difficult role. Of course, the form is just HTML,
but for me it is something more abstract.
In fact, the form is HTML, by means of which a user enters and receives data, but in addition form still performs
normalization, validation, filtering data and displaying error messages, if any.
This may require a rather significant amount of code.
If You can automate all these things, then You will only need configuration.
Each form consists of elements, each element has several attributes that override functionality
form.
Let's look at some of these aspects.
the Create form
Form is an instance of Zend_Form class. You can create an instance in many ways.
I prefer to create my classes and inherits the Zend_Form class. And in your classes make me change.
Each form consists of several important fields (the action method) and contains at least one element.
Let's see how you can create an item.
Each item may have some typical attributes: a field type, label, name and description.
Let's describe such an element in ZF.
Please note, there are several different ways of creating 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.
Approx. interpreter:
field type — Zend_Form_Element_Text
label (label) — 'Username'
name: 'username'
description (description) — 'This is a description'
In addition to the above there are a few important attributes of the item. 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.
the Filters and validators
Building chains of filters and validators is one of the very useful features of the component shapes.
The filter is the sort of thing that automatically performs conversion on the input data.
A validator is a mechanism for automatic data validation during form processing.
These mechanisms greatly simplify the controller code and the model, since the logic is moved to a more organized
structure.
Let's consider an example. On the registration form is the input element user name. Need to implement validation
the uniqueness of the name must only consist of permitted characters and its length must enter in a valid range.
If You have on the form is the element in which the user can enter arbitrary text, such as about yourself, then You certainly
want to apply filters to ensure that you have entered the "clean" data.
It would be useful to add filters of the type StripTags to remove all HTML tags, StringTrim, to remove all extra spaces.
If you wish, you can add filters to handle the BB-codes.
For this you can create your own filter or use the Callback filter that can be used in other forms.
The idea was to check the data as thoroughly as possible, You need to be sure that data is 100% consistent with your
Exercise maximum caution.
Filters help maintain the integrity of the data can also be used in the process of cleaning the data entered in the form.
If You allow the user to enter something, be sure to remove all HTML tags. I think You get the idea...
Provide 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.
the Skinny controller, fat model
For the form described above, You can develop a very simple controller.
I believe that the controller should manage the application logic, but should not work with the data.
Here is a sample action (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 finishes the input, the validator checks the entered information.
If the test ends successfully, the array data is passed to the model. By the time of data transmission in all model
filters and validators should finish the job. Now we already know how to format the data and how to improve their security.
In the controller You can redirect user to another page or to perform other actions in accordance with the logic of Your application.
A form is displayed to the user through the view. In the General case, the representation simply loads the form and displays it to the user.
If the user entered data contains an error, the view not only displays the form and display error messages, but
and will show the entered data.
the Scripts views, and decorators
Shown below is the entire code representation, which can be used to display the form. If additional configuration is required form, it can
to run 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 experience any difficulties with the imposition of additional
CSS styles. Using CSS fits well with the concept of separation of different functional parts of the application.
However, you often need to change not only the display style and position of elements. Zend for this purpose,
the Interaction models
In accordance with the code of our controller model is passed to the function $form->getValues().
The result of this function is pre-tested and the 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 fine. We work with a very simple interface, and we don't have to manually specify each value,
we want to convey. Of course, it's up to, but I don't understand people who passed in model 5-10 values.
These controllers will certainly be great.
Of course, the code does not depend on the shape, because it works with the array data. It makes the call model is very simple.
Model is the place where you want to start complex code.
With this approach, we can use our adapter object database or some other libraries.
the Conclusion
Comonent form (form) is one of the biggest, and perhaps the largest component in Zend. Don't 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, to me this is
a useful component in Zend. I strongly recommend to invest time to study it.
You may be interested in the material on caching forms.
Petrulevich Sergei
petrelevich@yandex.ru
www.SmartyIT.ru
Комментарии
Отправить комментарий