ZF & Zend_Form

07May08

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) {

parent::__construct($options);

$this->setName('register');
$this->setAttrib('enctype','multipart/form-data');

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

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

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

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

$state = new Zend_Form_Element_Select('stateID');
$state->setLabel('State');
$state->addMultiOption('1','Texas');
$state->setAttrib('disabled', 'disabled');
$fields[] = $state;

$zip = new Zend_Form_Element_Text('zip');
$zip->setLabel('Zip');
$zip->setAttrib('class', 'il');
$zip->setAttrib('size',7);
$zip->setRequired(true);
$zip->addValidator($empty);
$zip->addValidator(new Zend_Validate_Int);
$fields[] = $zip;

$website = new Zend_Form_Element_Text('website');
$website->setLabel('Website');
$website->addValidator($empty);
$fields[] = $website;

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

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

$this->addElements($fields);

$this->clearDecorators();
$this->addDecorator('FormElements')
->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!



3 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. 2 joshteam

    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->setName(’category’);
    $this->setAttrib(’enctype’, ‘multipart/form-data’);

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

Leave a Reply