Basics of Zend Form

To get Start working on zend FORM then just follow these things

1. Initialize the following object in your controller page

1
$form = new Zend_Form();

Before reading any further ,let me remind you one thing , Framework is nothing but just a deafult way that every programmer should follow at the time of writing of code. Zend form is nothing more than the OLD html form .Every thing is just same .just Recall at the time of  html form declaration what you are doing.

Giving the name of the form

Giving the method and action of the form

We are just doing the same as per new Zend_Form is conserned

2.Define properties of the zend form

1
2
3
4
5
6
7
$form = new Zend_Form();
 
$form->setName("formname goes here");
 
$form->setAction("nextpage.php");
 
$form->setMethod("post");

3. Lets write the previous code in more better way

1
2
3
4
5
6
7
$form = new Zend_Form();
 
$form->setName("formname goes here")
 
->setAction("nextpage.php")
 
->setMethod("post");

what i did here, if you see the code i have removed the $form variable from last 2 line and just put semicolon at the last line.

if a form is there,then there must of form elements like textbox,textarea,checkbox and dropdown list.Lets start how to create element using zend form.

4.Create a textbox using zend form.

We can create form element using the Form object, in the previous code $form is the “Zend Form” object.

1
2
3
4
5
6
7
8
9
$form = new Zend_Form();
 
$form->setName("formname goes here")
 
->setAction("nextpage.php")
 
->setMethod("post");
 
$username = $form->createElement('text', 'username');

Don’t even try to echo this line, still it not ready .We are just in a process of creating a complete form. At the time of creating a textbox we should set label of textbox.Then you must be thinking what is label.Label is something that will tell the user what should he/she should enter to the text box.

5. Add Label to the textbox.

1
2
3
4
5
6
7
8
9
10
11
$form = new Zend_Form();
 
$form->setName("formname goes here")
 
->setAction("nextpage.php")
 
->setMethod("post");
 
$username = $form->createElement('text', 'username')
 
->setLabel('Enter Username');

6. Store the elements and Display it in view page

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
$form = new Zend_Form();
 
$form->setName("formname goes here")
 
->setAction("nextpage.php")
 
->setMethod("post");
 
$username = $form->createElement('text', 'username')
 
->setLabel('Enter Username');
 
$form->addElement($username);
 
$this->view->form = $form;

Now goto you view page and write down the following code

1
2
3
4
5
<!--?php
 
echo $this--->form;
 
?>