Zend Form

Zend Form provide very useful functionalities, like

  • Easy way to create html forms and its elements.
  • Built-in validation
  • Built-in error messages
  • Filtering

    And so on.

Here I will give you some basic examples and provide you better approach for using Zend Forms.

Let us first see some examples of how and where we can create our Zend form.

The first place I mention would definitely be Controller/Action.

If you have

<?php

require_once(‘Zend/Form.php’);

class IndexController extends Zend_Controller_Action

{

function indexAction()
{
}

<p style=”margin-left:30px”
}

The require statement is compulsory if you don’t have

Zend_Loader::registerAutoload();

In your bootstrap.

The easiest way to create your zend form would be to place the following code into your indexAction method.

$form = new Zend_Form();

The above statement will create instance of the Zend_Form.

Once you create zend form instance, you will have access to all the functionality Zend Form is providing- we have mentioned some in the beginning, you can now call Zend_Form functions with $this.

Next step should be to add elements to your form.

Zend provide different ways to create and add elements to your form. E.g.

First create the element as

$username= new Zend_Form_Element_Text(“username”);

And set its label as

$username->setLabel(“Username:”);

Now add

$form->addElement($username);

Similarly

$submit = new Zend_Form_Element_Submit(“submit”);

$submit->setLabel(“Submit”);

$form->addElement($submit);

You can create as many form element as you like and add it to your form.

Once your form is created, assign it to your view template as

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

The only thing you would need to do now is to display your form in your view-view/scripts/index/index.phtml, file as

form;

?>

This will display form as

You can create Zend Form as

$form = new Zend_Form();

$form->addElement(

“text”,

“username”,

array(

“label” => “Username:”

)

);

$form->addElement(

“submit”,

“submit”,

array(

“label” => “Submit”

)

);

This code will create similar form as above.

Athough you can create Zend form in your controller/actions, however this is not good approach to be used. The best approach would be to create separate directory for your foms and create separate php file for each form. I would recommend the following directory structure

application/

controllers/

models/

forms/

views/

If you have login form, create Login.php in “forms” directory and place the following code

<?php

class forms_Login extends Zend_Form

{

public function __construct(options=array())

{

parent::__construct($options);

$this->setAction(“your action here”); // this define form action attribute.

$this->setMethod(‘post/get’);// set method attribute.

// the code bellow will create element

$username = $this->CreateElement(‘text’,’username’);

$username->setLabel(“Username:”);

// and

$submit= $this->CreateElement(“submit”,”submit”);

$submit->setLabel(“Submit”);

// now add elements to the form as

$this->addElements(array(

$username,

$submit

));

}

}

?>
The above code is very simple to explain. We are creating our class extending it from Zend_Form-it is necessary to prefix your form name with the directory name(forms_Login). By doing so we are inheriting all the functionality of the Zend_Form.

Next we are defining our constructor. It is necessary to call parent constructor before defining anything.

Another approach can be to override Zend_Form init() method and put the above code in that function.

Now once you create your form, next step is to create its instance in your controller/action and assign it to the view as

$form = new Login();

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

5 Responses to “Zend Form”

  1. Asim Zeeshan February 26, 2009 at 2:55 pm #

    I think, this should be a post instead of a PAGE

  2. Salman Ali March 11, 2009 at 9:28 am #

    Can you make some explanation for the zendx_JQuery.

    how to use it for making Forms..but plz make it easy example coz It is alredy explaned in Manual and hard to understand.

    Hope to get it soon …

  3. Faheem Abbas March 11, 2009 at 10:48 am #

    Well, i’ll definitely explain how to make JQuery form in Zend Framework. Its fun to implement it using jquery helpers provided with latest ZF version.

  4. sunil June 15, 2009 at 9:33 am #

    all detail information give me plz.

  5. Muhammad Atiq khattak December 4, 2009 at 6:23 am #

    this zend_form creation mathod in zend framework is so superior and soo simple that a dummies person can understand and create. i apperciate ur efforts.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.

Join 33 other followers

%d bloggers like this: