ZF & Zend_Form


So I’ve been using ZF more and more, and have come across another reason Zend Framework is just superior imo. Zend Form!

All developers have had the same issues with Forms, and keeping it a) reusable, b) clean, c) contained. Zend_Form does this all. At first it may seem a little confusing, especially if you aren’t familiar with OOP and Decorators, but it’s just another opportunity you get to learn by example. Here is a simple form of mine that lets you add a club, and it’s located in: Project/app/forms/

class forms_AddClub extends Zend_Form {
public function __construct($options = null) {



$fields = array();
$empty = new Zend_Validate_NotEmpty();
$empty->setMessage('is required');

$name = new Zend_Form_Element_Text('name');
$name->setLabel('Club Name');
$fields[] = $name;

$addy = new Zend_Form_Element_Text('address');
$addy->setLabel('Street Address');
$fields[] = $addy;

$city = new Zend_Form_Element_Select('cityID');
$city->setMultiOptions(array('1'=>'Dallas', '2'=>'Addison'));
$fields[] = $city;

$state = new Zend_Form_Element_Select('stateID');
$state->setAttrib('disabled', 'disabled');
$fields[] = $state;

$zip = new Zend_Form_Element_Text('zip');
$zip->setAttrib('class', 'il');
$zip->addValidator(new Zend_Validate_Int);
$fields[] = $zip;

$website = new Zend_Form_Element_Text('website');
$fields[] = $website;

$desc = new Zend_Form_Element_Textarea('description');
$desc->setAttrib('cols', 27);
$desc->setAttrib('rows', 6);
$fields[] = $desc;

$submit = new Zend_Form_Element_Submit('submit');
$submit->setLabel('Add Club!');
$fields[] = $submit;


->addDecorator('HtmlTag', array('tag' => '

    ')) ->addDecorator('Form'); $this->setElementDecorators(array( array('ViewHelper'), array('Description'), array('Label', array('separator'=>'')), array('HtmlTag', array('tag' => 'li')) )); // buttons do not need labels $submit->setDecorators(array( array('ViewHelper'), array('Description'),
array('HtmlTag', array('tag' => 'li', 'class'=>'submit-group')),
    )); } }

Then all I do in the controller is $form = new Forms_AddClub(); and I can call methods like $form->isValid(); or just echo $form and I am done! Yeah, it’s nice!

6 Responses to “ZF & Zend_Form”

  1. 1 Amit

    How could we give code to display form on html page?
    I have tried with
    <form id=”name;?>” enctype=”form->enctype;?>” action=”” method=”post”>

    but I’m not getting name and enctype on form.

  2. To show a form, in the view you just echo out the form object:

    //In the controller
    $form = new Your_Form();
    $this->view->assign(‘form’, $form);

    //In your view
    echo $this->form;

  3. 3 Amit

    Hi Josh,

    Thanx for reply.

    echo $this->form; returns complete form which we have define in controller. But I want to create .phtml manually. I’ve created one form for photo uploading in edit section I want to show img tag for displaying photo. Above form not allow me to put img tag near file controller. That reason I’ve created .phtml manually with out $this->form. Here I have added following tag in phtml file.

    <form id=”???” enctype=”??” action=”url(array(‘action’=>’edit’)); ?>” method=”post” >

    How could I get id and enctype which i have define in modal

    $this->setAttrib(‘enctype’, ‘multipart/form-data’);

    I would appreciate if you can give me answer.
    Thanks in advance.

  4. 4 Zeno

    How about a customisable view phtml for Zend_Form? Without setDecorator()


  5. 5 sreenu


    How can i write code by using Zend_Form for the above code .any one please help me

  6. 6 sreenu

    * should be in red color

Leave a Reply to joshteam Cancel 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 )

Google photo

You are commenting using your Google 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 )

Connecting to %s

%d bloggers like this: