Complex custom elements in Zend_Form

October 23, 2008 – 9:33 am Tags:

I’ve sometimes needed custom elements in forms – say two fields for inputting two numbers. For something simple like that, adding two separate elements usually suffice, but when one of my clients wanted a field with multiple checkboxes, but only specific pairs could be selected, it started getting too complex.

Until then, I had kind of avoided making those custom elements, as there didn’t seem to be much information on them in the Zend_Form documentation. I decided it was time to find out how to do it!

Alternatives

The alternatives would be creating custom view helpers to output the custom form elements, and using the viewscript decorator. Creating a custom view helper would also require a custom form element class, and it would be a bit tricky.

The viewscript decorator can be used to easily output elements with totally custom markup. You can even have the viewscript insert some JavaScript code into the inlineScript or headScript helpers. You could do this with just one of the default form elements, but in my case I required a custom element too.

I think the viewscript approach is the most flexible and simplest to implement, so I chose to go with that.

Implementing a custom time element

As an example, let’s look at how to implement a custom time element.

We’ll want to store the time in a database, in the following format: hh:mm:ss. We also want to make the user type each number in a separate box, so that’ll be three input boxes.

We could achieve this by simply adding three elements, but what if we needed to use this time box in more than one or two forms? We’d have a lot of forms where we’d need a same looking time field, and if we ever wanted to change how it works, we’d need to go through them all and manually change those three, so it’s much better to just make it a single element that we can change in one shot if required.

First, let’s look at the new form element class:

<?php
class CU_Form_Element_Time extends Zend_Form_Element 
{
    public function init()
    {
        parent::init();
        $this->addDecorator('ViewScript', array(
            'viewScript' => 'time.phtml'
        ));
 
        $this->addValidator(new Zend_Validate_Regex('/^[0-9]+:[0-9]+:[0-9]+$/'));
    }
 
    public function setValue($value)
    {
        if(is_array($value))
        {
            @list($hours, $minutes, $seconds) = $value;			
            $value = sprintf('%s:%s:%s', $hours, $minutes, $seconds);
        }
 
        return parent::setValue($value);
    }
}

In the init method, we set the viewscript’s name and add a validator, which makes sure the value fits into our format. Then, we override setValue, and in case of an array, we modify it into a string. Why? Because in our view script, we will have three boxes and they will be sent as an array in POST, and when the element is being validated, it will call setValue to set its value first.

We could also override isValid and add similar code to it, but I decided to use setValue, as now we could manually set the value from an array as well.

Next, the view script:

<?php @list($hours, $minutes, $seconds) = explode(':', $this->element->getValue()); ?>
<dt>
    <?= $this->formLabel($this->element->getName(), $this->element->getLabel()); ?>
</dt>
<dd>
    <input id="<?= $this->element->getName(); ?>" 
           type="text"
           name="<?= $this->element->getName(); ?>[]" 
           value="<?= $hours; ?>" />
 
    <input type="text" 
           name="<?= $this->element->getName(); ?>[]" 
           value="<?= $minutes; ?>" /> 
 
    <input type="text" 
           name="<?= $this->element->getName(); ?>[]" 
           value="<?= $seconds; ?>" />
 
    <?php if(count($this->element->getMessages()) > 0): ?>
        <?= $this->formErrors($this->element->getMessages()); ?>
    <?php endif; ?>
</dd>

First, we explode the values for the fields from the element’s value. The rest is quite usual. We need to add the dt and dd elements in, as the other decorators will not run with the viewscript decorator. Note how the name attributes have [] in the end? This makes the values from all of the inputs get placed in an array, so that we can easily access them in the element class.

To make the element work in your forms, you’ll need to add the path to it in the form prefix paths:

$this->addPrefixPath('CU_Form_Element_', 'CU/Form/Element/', Zend_Form::ELEMENT);

To make the viewscript work, you’ll need to place it in one of your views/scripts directories. It might be a good idea to create a common viewscript dir, say, application/views/scripts.

Conclusion

While not always necessary, custom form elements can make things easier to maintain and reuse. They may be a bit tricky to figure out at first, but after you get the idea, they are easy to create – just like Zend_Form decorators.

Further reading:
Autogenerating forms from Doctrine models
Another idea for using models with forms

Share this:
  1. 10 Responses to “Complex custom elements in Zend_Form”

  2. You could generalize a bit more I think.
    Now you assume the elements of the form are wrapped in dt and dd tags. Which doesn’t have to be the case.

    My ZF knowledge is a bit rusty right now (doing some django projects) but I think it should be possible.

    By Harro on Oct 23, 2008

  3. You’re right. You don’t have to wrap them in dts and dds – If your forms use a different style of markup, just modify the viewscript to output the correct kind of markup for your own case.

    By Jani Hartikainen on Oct 23, 2008

  4. Where do you recommend keeping custom elements in the MVC structure? /Library? or /application/forms? /Public/?

    By Altergothen on May 26, 2009

  5. Wheeeereeeee?

    By Mark on Jul 18, 2009

  6. Thank you for the post! It saved my day.

    By ion7 on Nov 23, 2009

  7. Thanks, this article helped me a lot. Keep doin’ this greate work!

    By Paulo on Nov 21, 2011

  1. 4 Trackback(s)

  2. Oct 24, 2008: Jani Hartikainen’s Blog: Complex custom elements in Zend_Form : WebNetiques
  3. Jan 20, 2009: Zend_Form decorator tips | CodeUtopia
  4. Jan 30, 2009: Client-side validation with Zend_Form | CodeUtopia
  5. Mar 10, 2009: Database backed Zend_Form elements | CodeUtopia

Post a Comment

You can use some HTML (a, em, strong, etc.). If you want to post code, use <pre lang="PHP">code here</pre> (you can replace PHP with the language you are posting)