ZF Simple Form Creation


 

ZF Simple Form Creation

In the current tutorial we will study how to create a simple form in Zend Framework. We will use two ways to create form, first one is pretty conventional way and other one is fully ZF style. So let's get our hands dirty with the first one....

In the current tutorial we will study how to create a simple form in Zend Framework. We will use two ways to create form, first one is pretty conventional way and other one is fully ZF style. So let's get our hands dirty with the first one....

ZF Simple form creation:

In the current tutorial we will study how to create a simple form in Zend Framework. We will use two ways to create  form, first one is pretty conventional way and other one is fully  ZF style. So let's get our hands dirty with the first one:

As we know that whenever we create an action in ZF, one phtml file automatically generates and we can use that phtml file as a simple html file. So, we can create a form in that file as we do in a simple html file. Remove the existing code with  simple html  coding which will create a form and modify as per your choice.

Next way to create a form is with Zend Framework. So, let's start by creating a form in the project directory, type the following code  in the command prompt, under the project directory:

zf create form Hello

Where Hello is the name of the form, after executing the above command, a form called Hello.php will be created inside the forms directory, if you are creating the form for the first time then forms directory will be created automatically as well.

Along with the form .zfproject.xml project profile will be updated.

Open the Hello.php file and paste the following coding:

<?php

class Application_Form_Hello extends Zend_Form

{

public function __construct($options=null)

{

parent::__construct($options);

$this->setName('Hello');

$name=new Zend_Form_Element_Text('Name');

$name->setLabel('Name')

->setRequired(true)

->addFilter('StripTags')

->addFilter('StringTrim')

->addValidator('NotEmpty');

$contact=new Zend_Form_Element_Text('Contact No.');

$contactr->setLabel('Contact No.')

->setRequired(true)

->addFilter('StripTags')

->addFilter('StringTrim');

0

$address=new Zend_Form_Element_Text('Address');

$address->setLabel('Address')

1

->setRequired(true)

->addFilter('StripTags')

->addFilter('StringTrim');

2

$submit=new Zend_Form_Element_Submit('submit');

3

$this->addElements(array($name,$contact,$address,$submit));

4

}

}

5

Now select a particular action method (page) in which you want to populate the form and append the following lines:

...

6

$form=new Application_Form_Login();

$this->view->form=$form;

To actually display the form we need to change the existing code of the page (say index.phtml) with:

7

<?php echo $this->form; ?>

 The above coding should display a form like the following:

ZF Form

8

If you are wondered with the color contrast of the above page ( http://roseindia.net/tutorial/php/zendframework/ZF-Decorate-With-CSS.html ), please visit our previous page which has a little description on CSS and ZF.

Ads