Make Zend_Form Bend To Your Will
Published: 09/23/2009
Programming, Code
The first thing you need when working with any new web language or framework is figure out how to work with forms. Forms are one of the constants when working online and it’s the rare occasion when you can bypass dealing with them. They are also one of the more rote and boring portions of a web app to build; they’re, pretty much, always the same requiring the same fields on the same pages with the same validation rules and the same database schema.
If you’re smart dealing with forms effectively becomes a priority. My previously held favorite method was using HTML_QuickForm for handling forms. If you’ve never used; IT IS AWESOME.
Here’s an example login form script:
<?php /** * Include HTML_QuickForm library */ require 'HTML/QuickForm.php'; /** * Include HTML_QuickForm Smarty Renderer */ require('HTML/QuickForm/Renderer/ArraySmarty.php'); $renderer =& new HTML_QuickForm_Renderer_ArraySmarty($smarty); $form = new HTML_QuickForm('login'); $form->addElement('text','Email','Email:' ,'class="textfield" size="30" maxlength="128"'); $form->addRule('Email','Please Enter Your Email','required', null, 'client'); $form->addRule('Email','Please enter a valid email address','email', true, 'client'); $form->addElement('password','Password','Password:','class="textfield" size="30" maxlength="128"'); $form->addRule('Password','Please Enter Your Password','required', null, 'client'); $form->addElement('checkbox','remember',null,'Remember Me?'); $form->addElement('submit','save','Submit','class="csubmit"'); if ($form->validate()) { //do the processing stuff here… } $form->accept($renderer); $smarty->assign('LoginForm', $renderer->toArray()); ?>
Very straightforward I think; instantiate the object, declare the elements, attach the rules and send to smarty (view) object.
The corresponding Smarty template is just as straightforward:
{$LoginForm.javascript} <form {$LoginForm.attributes}> {$LoginForm.hidden} {if $LoginForm.Email.error}<span class="errorMessage">{$LoginForm.Email.error}</span>{/if} <div id="email"> Email: {$LoginForm.Email.html} </div> <div id="password"> Password: {$LoginForm.Password.html} </div> <div id="remember"> {$LoginForm.remember.html} </div> <div id="submit"> {$LoginForm.submit.html} </div> </form>
That’s my preferred method when layout of the form is important and/or outside the box; it’s totally possible to use HTML_QuickForm without the Smarty integration but I like the control. It allows me to build forms as complicated in layout as the I Am Legend community was (my baseline for form complexity):
Since I’m now using the Zend Framework, and it’s form component is Zend_Form, I can honestly say after the flexibility of HTML_QuickForm and Smarty Zend_Form is, at first, not so awesome. A lot of the resources and tutorials/articles for Zend_Form tout it’s use of the decorator design pattern and all the examples I found looked the same. Not. Good.
Almost none of the references I looked at mentioned anything about removing decorators and rendering just the raw form elements like I needed. It is possible though; you just have to attache a couple extra methods to remove the decorators as laid out below. Make note of the calls to “removeDecorator” and “setAttrib”. Those are the key pieces.
As a counter point here’s the above script using Zend_Form:
<?php //APPLICATION_PATH/forms/Login.php <?php class Form_Login extends Zend_Form { public function __construct($options = null) { parent::__construct($options); $email = new Zend_Form_Element_Text('email'); $email->setLabel('Email') ->setRequired(true) ->addFilter('StripTags') ->addFilter('StringTrim') ->addValidator('NotEmpty', TRUE) ->addValidator(new Zend_Validate_Db_RecordExists('users', 'email')) ->removeDecorator('label') ->removeDecorator('htmlTag') ->removeDecorator('description') ->setAttrib('class', 'transperentInput'); $password = new Zend_Form_Element_Password('password'); $password->setLabel('Password') ->setRequired(true) ->addFilter('StripTags') ->addFilter('StringTrim') ->addValidator('NotEmpty') ->removeDecorator('label') ->removeDecorator('htmlTag') ->removeDecorator('description') ->setAttrib('class', 'transperentInput'); $submit = new Zend_Form_Element_Submit('submit'); $this->addElements(array($email, $password, $submit)); } } ?>
Since Zend_Form is a part of the Zend Framework MVC the above is just the form class and the below is the actual processing portion:
<?php //APPLICATION_PATH/controllers/Login.php class LoginController extends Zend_Controller_Action { public function indexAction() { $form = new Form_Login; if ($this->getRequest()->isPost()) { $formData = $this->getRequest()->getPost(); if ($form->isValid($formData)) { //do the processing stuff here… } } $this->view->form = $form; } } ?>
Sigh…
And then in the view script:
<form action="<?php echo $this->escape($this->form->getAction()); ?>" method="<?php echo $this->escape($this->form->getMethod()) ?>" id="sumbitForm"> <div id="email"> Email: <?=$this->form->email; ?> </div> <div id="password"> Password: <?=$this->form->password; ?> </div> <div id="remember"> <?=$this->form->remember; ?> </div> <div id="submit"> <?=$this->form->submit; ?> </div> </form>
There you go: Zend_Form is your bitch.