Zend Framework

Archive for March 4th, 2009

Zend Framework: Creating custom Zend form

Posted by Faheem Abbas on March 4, 2009

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

Posted in Zend Framework | 2 Comments »

Does hard work give benefits?

Posted by Faheem Abbas on March 4, 2009

This is not always true, in my case.

I’m working hard, however I’ve been having rebound headache since three weeks and it has

scared me lot. Honestly and frankly I was feeling pain, look like my head will blow up.

What a programmer can do other then google his bug.

This is when bug missed up everything. The bug now wasn’t in my code by instead in my body.

Seem funny a bit, I guess.

I did the same, googled my bug, as anyhow I am bloody hell programmer. Yes exactly, I mean bloody hell.

it give lot of pain to be a programmer, as it was proved yesterday.

After googling and visiting few sites I come to the conclusion that several things like

digestion specially eating spicy and sour foods/fruits, over usage of pain killers, stress and tension, more usage of computer, Watching Tv all the day  etc cause rebound headache.

The point that made me wonder was “over usage of computer”.

I feel this is why I got rebound headache quite often.

Since last few weeks I’m not only having tough time in office, but also trying to learn

java, forcing me to spend more time on system.

I forgot one thing, that shouldn’t be.

You know what, huh?

It may not be really interesting for you, by the way. However I am also writing articles

time by time on different topics and posting on my blog.

Isn’t it of your interest?

It really shouldn’t be if you are not working in Zend Framework and haven’t visited my blog.

What I’ve planned to overcome this problem?

I don’t know, however someone may suggest that I should meet my physician. I think I’ll do the same.

Posted in Personal | 4 Comments »