Extending Zend_Form
Zend_Form is one of the key Zend Framework components. It greatly simplifies handling of HTML forms and can cooperate with other Zend Framework components such as Zend_Filter, Zend_Validate or Zend_View.
There are other ways to use it but I usually extend the Zend_Form class and create a separate class for each form in my application. Moreover, I almost exclusively use the MVC design pattern and therefor I use Zend_Form together with Zend_View.
Build a form
Here’s an example of a simple contact form:
-
class Contact extends Zend_Form
-
{
-
public function init()
-
{
-
$this->setMethod('post');
-
-
$name = new Zend_Form_Element_Text('name', array(
-
'label' => 'Your name',
-
'required' => true,
-
'filters' => array(
-
'StringTrim'
-
),
-
'validators' => array(
-
array('StringLength', false, array(3, 50))
-
),
-
'class' => 'input-text'
-
));
-
-
$email = new Zend_Form_Element_Text('email', array(
-
'label' => 'Your email',
-
'required' => true,
-
'filters' => array(
-
'StringTrim'
-
),
-
'validators' => array(
-
'EmailAddress'
-
),
-
'class' => 'input-text'
-
));
-
-
$subject = new Zend_Form_Element_Text('subject', array(
-
'label' => 'Subject',
-
'required' => true,
-
'filters' => array(
-
'StringTrim'
-
),
-
'validators' => array(
-
array('StringLength', false, array(3, 50))
-
),
-
'class' => 'input-text'
-
));
-
-
$message = new Zend_Form_Element_Textarea('message', array(
-
'label' => 'Message',
-
'rows' => 10,
-
'cols' => 50,
-
'required' => true,
-
'filters' => array(
-
'StringTrim'
-
),
-
'validators' => array(
-
array('StringLength', false, array(20, 1500))
-
)
-
));
-
-
$captcha = new Zend_Form_Element_Captcha('captcha', array(
-
'label' => 'Are you a human?',
-
'helper' => null,
-
'captcha' => array(
-
'captcha' => 'Figlet',
-
'wordLen' => 6,
-
),
-
'class' => 'input-text'
-
));
-
-
$submit = new Zend_Form_Element_Submit('contact', array(
-
'label' => 'Submit',
-
'class' => 'input-submit'
-
));
-
-
$this->addElements(array($name, $email, $subject, $message, $captcha, $submit));
-
}
-
}
The first thing I did was to set a HTTP method to POST with setMethod(). Other option is to use HTTP GET. Next I created an object for every form element. As you can see, there are three input text fields (name, email, subject), one textarea (message), a captcha and a submit button.
Zend Framework contains classes for all HTML form elements you could ever need plus also a wide range of captcha classes. Every element object constructor takes two arguments: the first is an HTML name attribute, the second is array of other HTML attributes such as label or class and additional attributes such as filters and validators.
What’s the difference between filters and validators?
- A filter normalizes input values to match an expected format. A typical example is the StringTrim filter which removes whitespace from the beginning and end of a string. Filters are executed before validation takes place.
- A validator tests input values against some criteria. Values are not changed and the validator reports back whether the input values passed the test or not. A typical example would be the EmailAddress validator which checks if an input value is a valid email address.
The last line of the class – addElements() – adds elements to the form. You can you use the addElement() method to add a single element, too.
Validate the form
First, you need to create an instance of the form class. I use a simple method to do that for me:
-
private function _getForm($form, $action)
-
{
-
include APPLICATION_PATH . '/modules/' . $this->_request_>getModuleName()
-
. '/forms/' . $form . '.php';
-
$form = new $form();
-
$form->setAction($action);
-
return $form;
-
}
Second, in a controller action you load the form and validate it:
-
public function contactAction()
-
{
-
// get the request object
-
$request = $this->getRequest();
-
-
// load the Contact form
-
$form = $this->_getForm('Contact',
-
$request->getActionName());
-
// if POST data has been submitted
-
if ($request->isPost()) {
-
// if the Contact form has been submitted and the submitted data is valid
-
if (isset($_POST['contact']) && $form->isValid($_POST)) {
-
-
// get the form values
-
$data = $form->getValues();
-
-
/*
-
* do something here
-
* for example, send an email using Zend_Mail
-
*/
-
-
/*
-
* reset the form fields
-
* in case a user resubmits the form
-
* (by hitting F5 for instance)
-
*/
-
$form->reset();
-
-
}
-
}
-
-
// pass the form to the view so it can be rendered
-
$this->view->form = $form;
-
}
Render the form
We have already passed the form to the view, so we can render it:
-
<h2>Contact form</h2>
-
<?php echo $this->form, "\n"; ?>
And how does it look like? Here is the result with almost no styling:

After you submit the form Zend_Form will take care of filtering and validating the input values and it will also render errors:

The default XHTML markup uses a definition list for form elements. Form elements are rendered like this:
<dt><label for="foo">Foo</label><dt> <dd> <input name="foo" id="foo" type="text" value="" /> <div><ul> <li>...</li> </ul></div> </dd>
That’s not a bad markup but it might not fit everybody’s needs (I use an ordered list instead of a definition list). Fortunately, you can customize the markup easily using decorators. Decorators? What’s that?
Well, Zend_Form uses the Decorator design pattern to build the XHTML markup. Decorators in Zend Framework are a little bit more advanced topic but they are still pretty easy to work with. I recommend reading this article to understand how they work.

One Comment
leave a commentTrackbacks and Pingbacks