As I have already written in one of my article that when I was developing my first application in Zend Framework, I was unable to use Zend Framework Zend_Form component.
However developing my second application in Zend I was forced by my client to use it. And honestly and frankly I found it and still finding it quite helpful and interesting. Although I have written an article on Zend_Form earlier, however It will not be possible to discuss every aspect of it in single article. In this article I will try to cover as much as possible, however I know It will still be hard to go through each and every corner of it.
You can start your Zend Framework form creation journey from different places. You can create it through ini files, using separate functions for it in your controllers, however I will like and recommend you to create a separate php files for each of your form in a separate directory.
Why I recommend separate directory and separate php files for Zend_Form?
Well, this is because I feel its easy to create and use them. Further more if you need similar form in different pages, you can call the same form in multiple places.
What directory structure I recommend?
I’ll recommend the following directory structure.
application/
controllers/
models/
forms/
views/
scripts/
library/
Zend/
wwwroot/
js/
css/
images/
index.php
I’ll create separate file for each form in my html_root/application/forms directory.
How to create first custom Zend Framework Form?
Its very easy to create Zend_Form. You will only need to extend your form from Zend Framework Zend_Form component and override init() method as
<?php
class CustomForm extends Zend_Form
{
public function init()
{
}
}
That’s it, we have now created our custom form. Keep in mind that we haven’t added anything to our form yet.
You can see how easy it is to create form in Zend Framework.
Next step is to add some elements to it.
In your init() method write the following statements.
$firstname = $this->createElement(‘text’,’firstname’);
$firstname->setLabel(‘First Name:’);
$this->addElement($firstname);
In the first line, I called the createElement() method of the Zend_Form passing two parameters “text” and “firstname”. The first parameter tell Zend_Form that I like to create text element of the html form and the second parameter is set as name and id of the form. In the next line I set the label to the form element and finally I add element to the form using addElement() method.
Using the above method you can create as many element as possible and add them to your custom Zend Form.
Another very nice thing, about Zend Framework form, I would like to discuss is that Zend provide separate class for different form elements like
$firstname = Zend_Form_Element_Text(‘firstname’);
$firstname->setLabel(‘First Name:’);
$this->addElement($firstname);
In the above code I used Zend_Form_Element_Text for creating text element of the form, passing element name/id as parameter.
The class available for creating different form elements are
Zend_Form_Element_Button
Zend_Form_Element_Captcha
Zend_Form_Element_Checkbox
Zend_Form_Element_File
Zend_Form_Element_Hidden
Zend_Form_Element_Hash
Zend_Form_Element_Image
Zend_Form_Element_MultiCheckbox
Zend_Form_Element_Multiselect
Zend_Form_Element_Password
Zend_Form_Element_Radio
Zend_Form_Element_Reset
Zend_Form_Element_Select
Zend_Form_Element_Submit
Zend_Form_Element_Text
Zend_Form_Element_Textarea
You can use either of the above to create each of these element.
Beside creating different form element you can add validator, filtors and decorator on each form element.
What are validator?
Validator are used to ensure that the submission is valid.
For example you want that the form specific elements shouldn’t be empty. i.e you don’t want the form to be submitted untill and unless some specific element are filled, or you may want the data entered in some elements must be alphbetic only etc. So validator come handly for this purpose.
How to add validators to the Form element?
Its very simple, write the following line of code
$firstname->addValidator(‘NotEmpty’);
This means that the form will not be submitted untill and unless you entered something in the firstname form element.
If you want that the data entered in the firstname should all be alphabetic characters. Then you will need to add the following line of code.
$firstname->addValidator(“Alpha”);
The most valuable example would be to validate a specific form element against valid email address. This is extremely easy in Zend Framework Zend_Form. Add only the following line of code.
$email->addValidator(‘EmailAddress’);
Following validators are shipped with Zend Framework.
Alnum
Alpha
Barcode
Between
Ccnum
Date
Digits
EmailAddress
Float
GreaterThan
Hex
Hostname
InArray
Int
Ip
LessThan
NotEmpty
Regex
StringLength