1 <?xml version="1.0" encoding="UTF-8"?>
3 <sect1 id="zend.form.advanced">
4 <title>Advanced Zend_Form Usage</title>
7 <classname>Zend_Form</classname> has a wealth of functionality, much of it aimed
8 at experienced developers. This chapter aims to document some of this
9 functionality with examples and use cases.
12 <sect2 id="zend.form.advanced.arrayNotation">
13 <title>Array Notation</title>
16 Many experienced web developers like to group related form elements
17 using array notation in the element names. For example, if you have
18 two addresses you wish to capture, a shipping and a billing address,
19 you may have identical elements; by grouping them in an array, you
20 can ensure they are captured separately. Take the following form,
24 <programlisting language="html"><![CDATA[
27 <legend>Shipping Address</legend>
29 <dt><label for="recipient">Ship to:</label></dt>
30 <dd><input name="recipient" type="text" value="" /></dd>
32 <dt><label for="address">Address:</label></dt>
33 <dd><input name="address" type="text" value="" /></dd>
35 <dt><label for="municipality">City:</label></dt>
36 <dd><input name="municipality" type="text" value="" /></dd>
38 <dt><label for="province">State:</label></dt>
39 <dd><input name="province" type="text" value="" /></dd>
41 <dt><label for="postal">Postal Code:</label></dt>
42 <dd><input name="postal" type="text" value="" /></dd>
47 <legend>Billing Address</legend>
49 <dt><label for="payer">Bill To:</label></dt>
50 <dd><input name="payer" type="text" value="" /></dd>
52 <dt><label for="address">Address:</label></dt>
53 <dd><input name="address" type="text" value="" /></dd>
55 <dt><label for="municipality">City:</label></dt>
56 <dd><input name="municipality" type="text" value="" /></dd>
58 <dt><label for="province">State:</label></dt>
59 <dd><input name="province" type="text" value="" /></dd>
61 <dt><label for="postal">Postal Code:</label></dt>
62 <dd><input name="postal" type="text" value="" /></dd>
67 <dt><label for="terms">I agree to the Terms of Service</label></dt>
68 <dd><input name="terms" type="checkbox" value="" /></dd>
71 <dd><input name="save" type="submit" value="Save" /></dd>
77 In this example, the billing and shipping address contain some
78 identical fields, which means one would overwrite the other. We can
79 solve this solution using array notation:
82 <programlisting language="html"><![CDATA[
85 <legend>Shipping Address</legend>
87 <dt><label for="shipping-recipient">Ship to:</label></dt>
88 <dd><input name="shipping[recipient]" id="shipping-recipient"
89 type="text" value="" /></dd>
91 <dt><label for="shipping-address">Address:</label></dt>
92 <dd><input name="shipping[address]" id="shipping-address"
93 type="text" value="" /></dd>
95 <dt><label for="shipping-municipality">City:</label></dt>
96 <dd><input name="shipping[municipality]" id="shipping-municipality"
97 type="text" value="" /></dd>
99 <dt><label for="shipping-province">State:</label></dt>
100 <dd><input name="shipping[province]" id="shipping-province"
101 type="text" value="" /></dd>
103 <dt><label for="shipping-postal">Postal Code:</label></dt>
104 <dd><input name="shipping[postal]" id="shipping-postal"
105 type="text" value="" /></dd>
110 <legend>Billing Address</legend>
112 <dt><label for="billing-payer">Bill To:</label></dt>
113 <dd><input name="billing[payer]" id="billing-payer"
114 type="text" value="" /></dd>
116 <dt><label for="billing-address">Address:</label></dt>
117 <dd><input name="billing[address]" id="billing-address"
118 type="text" value="" /></dd>
120 <dt><label for="billing-municipality">City:</label></dt>
121 <dd><input name="billing[municipality]" id="billing-municipality"
122 type="text" value="" /></dd>
124 <dt><label for="billing-province">State:</label></dt>
125 <dd><input name="billing[province]" id="billing-province"
126 type="text" value="" /></dd>
128 <dt><label for="billing-postal">Postal Code:</label></dt>
129 <dd><input name="billing[postal]" id="billing-postal"
130 type="text" value="" /></dd>
135 <dt><label for="terms">I agree to the Terms of Service</label></dt>
136 <dd><input name="terms" type="checkbox" value="" /></dd>
139 <dd><input name="save" type="submit" value="Save" /></dd>
145 In the above sample, we now get separate addresses. In the submitted
146 form, we'll now have three elements, the 'save' element for the
147 submit, and then two arrays, 'shipping' and 'billing', each with
148 keys for their various elements.
152 <classname>Zend_Form</classname> attempts to automate this process with its
153 <link linkend="zend.form.forms.subforms">sub forms</link>. By
154 default, sub forms render using the array notation as shown in the
155 previous <acronym>HTML</acronym> form listing, complete with ids. The array name is
156 based on the sub form name, with the keys based on the elements
157 contained in the sub form. Sub forms may be nested arbitrarily deep,
158 and this will create nested arrays to reflect the structure.
159 Additionally, the various validation routines in
160 <classname>Zend_Form</classname> honor the array structure, ensuring that your
161 form validates correctly, no matter how arbitrarily deep you nest
162 your sub forms. You need do nothing to benefit from this; this
163 behaviour is enabled by default.
167 Additionally, there are facilities that allow you to turn on array
168 notation conditionally, as well as specify the specific array to
169 which an element or collection belongs:
175 <methodname>Zend_Form::setIsArray($flag)</methodname>: By setting the
176 flag <constant>TRUE</constant>, you can indicate that an entire form should be
177 treated as an array. By default, the form's name will be
178 used as the name of the array, unless
179 <methodname>setElementsBelongTo()</methodname> has been called. If the
180 form has no specified name, or if
181 <methodname>setElementsBelongTo()</methodname> has not been set, this
182 flag will be ignored (as there is no array name to which
183 the elements may belong).
187 You may determine if a form is being treated as an array
188 using the <methodname>isArray()</methodname> accessor.
194 <methodname>Zend_Form::setElementsBelongTo($array)</methodname>:
195 Using this method, you can specify the name of an array to
196 which all elements of the form belong. You can determine the
197 name using the <methodname>getElementsBelongTo()</methodname> accessor.
203 Additionally, on the element level, you can specify individual
204 elements may belong to particular arrays using
205 <methodname>Zend_Form_Element::setBelongsTo()</methodname> method.
206 To discover what this value is -- whether set explicitly or
207 implicitly via the form -- you may use the
208 <methodname>getBelongsTo()</methodname> accessor.
212 <sect2 id="zend.form.advanced.multiPage">
213 <title>Multi-Page Forms</title>
216 Currently, Multi-Page forms are not officially supported in
217 <classname>Zend_Form</classname>; however, most support for implementing them
218 is available and can be utilized with a little extra tooling.
222 The key to creating a multi-page form is to utilize sub forms, but
223 to display only one such sub form per page. This allows you to
224 submit a single sub form at a time and validate it, but not process
225 the form until all sub forms are complete.
228 <example id="zend.form.advanced.multiPage.registration">
229 <title>Registration Form Example</title>
232 Let's use a registration form as an example. For our purposes,
233 we want to capture the desired username and password on the
234 first page, then the user's metadata -- given name, family name,
235 and location -- and finally allow them to decide what mailing
236 lists, if any, they wish to subscribe to.
240 First, let's create our own form, and define several sub forms
244 <programlisting language="php"><![CDATA[
245 class My_Form_Registration extends Zend_Form
247 public function init()
249 // Create user sub form: username and password
250 $user = new Zend_Form_SubForm();
251 $user->addElements(array(
252 new Zend_Form_Element_Text('username', array(
254 'label' => 'Username:',
255 'filters' => array('StringTrim', 'StringToLower'),
256 'validators' => array(
260 array('/^[a-z][a-z0-9]{2,}$/'))
264 new Zend_Form_Element_Password('password', array(
266 'label' => 'Password:',
267 'filters' => array('StringTrim'),
268 'validators' => array(
270 array('StringLength', false, array(6))
275 // Create demographics sub form: given name, family name, and
277 $demog = new Zend_Form_SubForm();
278 $demog->addElements(array(
279 new Zend_Form_Element_Text('givenName', array(
281 'label' => 'Given (First) Name:',
282 'filters' => array('StringTrim'),
283 'validators' => array(
286 array('/^[a-z][a-z0-9., \'-]{2,}$/i'))
290 new Zend_Form_Element_Text('familyName', array(
292 'label' => 'Family (Last) Name:',
293 'filters' => array('StringTrim'),
294 'validators' => array(
297 array('/^[a-z][a-z0-9., \'-]{2,}$/i'))
301 new Zend_Form_Element_Text('location', array(
303 'label' => 'Your Location:',
304 'filters' => array('StringTrim'),
305 'validators' => array(
306 array('StringLength', false, array(2))
311 // Create mailing lists sub form
312 $listOptions = array(
313 'none' => 'No lists, please',
314 'fw-general' => 'Zend Framework General List',
315 'fw-mvc' => 'Zend Framework MVC List',
316 'fw-auth' => 'Zend Framwork Authentication and ACL List',
317 'fw-services' => 'Zend Framework Web Services List',
319 $lists = new Zend_Form_SubForm();
320 $lists->addElements(array(
321 new Zend_Form_Element_MultiCheckbox('subscriptions', array(
323 'Which lists would you like to subscribe to?',
324 'multiOptions' => $listOptions,
326 'filters' => array('StringTrim'),
327 'validators' => array(
330 array(array_keys($listOptions)))
335 // Attach sub forms to main form
336 $this->addSubForms(array(
346 Note that there are no submit buttons, and that we have done
347 nothing with the sub form decorators -- which means that by
348 default they will be displayed as fieldsets. We will need to be
349 able to override these as we display each individual sub form,
350 and add in submit buttons so we can actually process them --
351 which will also require action and method properties. Let's add
352 some scaffolding to our class to provide that information:
355 <programlisting language="php"><![CDATA[
356 class My_Form_Registration extends Zend_Form
361 * Prepare a sub form for display
363 * @param string|Zend_Form_SubForm $spec
364 * @return Zend_Form_SubForm
366 public function prepareSubForm($spec)
368 if (is_string($spec)) {
369 $subForm = $this->{$spec};
370 } elseif ($spec instanceof Zend_Form_SubForm) {
373 throw new Exception('Invalid argument passed to ' .
374 __FUNCTION__ . '()');
376 $this->setSubFormDecorators($subForm)
377 ->addSubmitButton($subForm)
378 ->addSubFormActions($subForm);
383 * Add form decorators to an individual sub form
385 * @param Zend_Form_SubForm $subForm
386 * @return My_Form_Registration
388 public function setSubFormDecorators(Zend_Form_SubForm $subForm)
390 $subForm->setDecorators(array(
392 array('HtmlTag', array('tag' => 'dl',
393 'class' => 'zend_form')),
400 * Add a submit button to an individual sub form
402 * @param Zend_Form_SubForm $subForm
403 * @return My_Form_Registration
405 public function addSubmitButton(Zend_Form_SubForm $subForm)
407 $subForm->addElement(new Zend_Form_Element_Submit(
410 'label' => 'Save and continue',
419 * Add action and method to sub form
421 * @param Zend_Form_SubForm $subForm
422 * @return My_Form_Registration
424 public function addSubFormActions(Zend_Form_SubForm $subForm)
426 $subForm->setAction('/registration/process')
434 Next, we need to add some scaffolding in our action controller,
435 and have several considerations. First, we need to make sure we
436 persist form data between requests, so that we can determine
437 when to quit. Second, we need some logic to determine what form
438 segments have already been submitted, and what sub form to
439 display based on that information. We'll use
440 <classname>Zend_Session_Namespace</classname> to persist data, which will
441 also help us answer the question of which form to submit.
445 Let's create our controller, and add a method for retrieving a
449 <programlisting language="php"><![CDATA[
450 class RegistrationController extends Zend_Controller_Action
454 public function getForm()
456 if (null === $this->_form) {
457 $this->_form = new My_Form_Registration();
465 Now, let's add some functionality for determining which form to
466 display. Basically, until the entire form is considered valid,
467 we need to continue displaying form segments. Additionally, we
468 likely want to make sure they're in a particular order: user,
469 demog, and then lists. We can determine what data has been
470 submitted by checking our session namespace for particular keys
471 representing each subform.
474 <programlisting language="php"><![CDATA[
475 class RegistrationController extends Zend_Controller_Action
479 protected $_namespace = 'RegistrationController';
483 * Get the session namespace we're using
485 * @return Zend_Session_Namespace
487 public function getSessionNamespace()
489 if (null === $this->_session) {
491 new Zend_Session_Namespace($this->_namespace);
494 return $this->_session;
498 * Get a list of forms already stored in the session
502 public function getStoredForms()
505 foreach ($this->getSessionNamespace() as $key => $value) {
513 * Get list of all subforms available
517 public function getPotentialForms()
519 return array_keys($this->getForm()->getSubForms());
523 * What sub form was submitted?
525 * @return false|Zend_Form_SubForm
527 public function getCurrentSubForm()
529 $request = $this->getRequest();
530 if (!$request->isPost()) {
534 foreach ($this->getPotentialForms() as $name) {
535 if ($data = $request->getPost($name, false)) {
536 if (is_array($data)) {
537 return $this->getForm()->getSubForm($name);
547 * Get the next sub form to display
549 * @return Zend_Form_SubForm|false
551 public function getNextSubForm()
553 $storedForms = $this->getStoredForms();
554 $potentialForms = $this->getPotentialForms();
556 foreach ($potentialForms as $name) {
557 if (!in_array($name, $storedForms)) {
558 return $this->getForm()->getSubForm($name);
568 The above methods allow us to use notations such as "<command>$subForm =
569 $this->getCurrentSubForm();</command>" to retrieve the current
570 sub form for validation, or "<command>$next =
571 $this->getNextSubForm();</command>" to get the next one to
576 Now, let's figure out how to process and display the various sub
577 forms. We can use <methodname>getCurrentSubForm()</methodname> to determine
578 if any sub forms have been submitted (<constant>FALSE</constant> return values
579 indicate none have been displayed or submitted), and
580 <methodname>getNextSubForm()</methodname> to retrieve a form to display. We
581 can then use the form's <methodname>prepareSubForm()</methodname> method to
582 ensure the form is ready for display.
586 When we have a form submission, we can validate the sub form,
587 and then check to see if the entire form is now valid. To do
588 these tasks, we'll need additional methods that ensure that
589 submitted data is added to the session, and that when validating
590 the form entire, we validate against all segments from the
594 <programlisting language="php"><![CDATA[
595 class RegistrationController extends Zend_Controller_Action
600 * Is the sub form valid?
602 * @param Zend_Form_SubForm $subForm
606 public function subFormIsValid(Zend_Form_SubForm $subForm,
609 $name = $subForm->getName();
610 if ($subForm->isValid($data)) {
611 $this->getSessionNamespace()->$name = $subForm->getValues();
619 * Is the full form valid?
623 public function formIsValid()
626 foreach ($this->getSessionNamespace() as $key => $info) {
630 return $this->getForm()->isValid($data);
636 Now that we have the legwork out of the way, let's build the
637 actions for this controller. We'll need a landing page for the
638 form, and then a 'process' action for processing the form.
641 <programlisting language="php"><![CDATA[
642 class RegistrationController extends Zend_Controller_Action
646 public function indexAction()
648 // Either re-display the current page, or grab the "next"
650 if (!$form = $this->getCurrentSubForm()) {
651 $form = $this->getNextSubForm();
653 $this->view->form = $this->getForm()->prepareSubForm($form);
656 public function processAction()
658 if (!$form = $this->getCurrentSubForm()) {
659 return $this->_forward('index');
662 if (!$this->subFormIsValid($form,
663 $this->getRequest()->getPost())) {
664 $this->view->form = $this->getForm()->prepareSubForm($form);
665 return $this->render('index');
668 if (!$this->formIsValid()) {
669 $form = $this->getNextSubForm();
670 $this->view->form = $this->getForm()->prepareSubForm($form);
671 return $this->render('index');
675 // Render information in a verification page
676 $this->view->info = $this->getSessionNamespace();
677 $this->render('verification');
683 As you'll notice, the actual code for processing the form is
684 relatively simple. We check to see if we have a current sub form
685 submission, and if not, we go back to the landing page. If we do
686 have a sub form, we attempt to validate it, redisplaying it if
687 it fails. If the sub form is valid, we then check to see if the
688 form is valid, which would indicate we're done; if not, we
689 display the next form segment. Finally, we display a
690 verification page with the contents of the session.
694 The view scripts are very simple:
697 <programlisting language="php"><![CDATA[
698 <?php // registration/index.phtml ?>
699 <h2>Registration</h2>
700 <?php echo $this->form ?>
702 <?php // registration/verification.phtml ?>
703 <h2>Thank you for registering!</h2>
705 Here is the information you provided:
709 // Have to do this construct due to how items are stored in session
711 foreach ($this->info as $info):
712 foreach ($info as $form => $data): ?>
713 <h4><?php echo ucfirst($form) ?>:</h4>
715 <?php foreach ($data as $key => $value): ?>
716 <dt><?php echo ucfirst($key) ?></dt>
717 <?php if (is_array($value)):
718 foreach ($value as $label => $val): ?>
719 <dd><?php echo $val ?></dd>
722 <dd><?php echo $this->escape($value) ?></dd>
731 Upcoming releases of Zend Framework will include components to
732 make multi page forms simpler by abstracting the session and
733 ordering logic. In the meantime, the above example should serve
734 as a reasonable guideline on how to accomplish this task for