PhpRiot
Become Zend Certified

Prepare for the ZCE exam using our quizzes (web or iPad/iPhone). More info...


When you're ready get 7.5% off your exam voucher using voucher CJQNOV23 at the Zend Store

Creating Forms Using Zend_Form

The Zend_Form class is used to aggregate form elements, display groups, and subforms. It can then perform the following actions on those items:

  • Validation, including retrieving error codes and messages

  • Value aggregation, including populating items and retrieving both filtered and unfiltered values from all items

  • Iteration over all items, in the order in which they are entered or based on the order hints retrieved from each item

  • Rendering of the entire form, either via a single decorator that performs custom rendering or by iterating over each item in the form

While forms created with Zend_Form may be complex, probably the best use case is for simple forms; its best use is for Rapid Application Development (RAD) and prototyping.

At its most basic, you simply instantiate a form object:

<?php
// Generic form object:
$form = new Zend_Form();

// Custom form object:
$form = new My_Form()

You can optionally pass in a instance of Zend_Config or an array, which will be used to set object state and potentially create new elements:

<?php
// Passing in configuration options:
$form = new Zend_Form($config);

Zend_Form is iterable, and will iterate over elements, display groups, and subforms, using the order they were registered and any order index each may have. This is useful in cases where you wish to render the elements manually in the appropriate order.

Zend_Form's magic lies in its ability to serve as a factory for elements and display groups, as well as the ability to render itself through decorators.

Zend Framework