Home Tutorial Php Zendframework ZF Simple Form Creation

 
 

Share on Google+Share on Google+
ZF Simple Form Creation
Posted on: April 29, 2010 at 12:00 AM
Advertisement
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');

$address=new Zend_Form_Element_Text('Address');

$address->setLabel('Address')

->setRequired(true)

->addFilter('StripTags')

->addFilter('StringTrim');

$submit=new Zend_Form_Element_Submit('submit');

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

}

}

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

...

$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:

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

 The above coding should display a form like the following:

ZF Form

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.

Advertisement

Related Tags for ZF Simple Form Creation:


Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.

Posted on: April 29, 2010

Recommend the tutorial

Advertisements Advertisements
 

 

 

DMCA.com