Zend_Form_Decorator_ViewScript

Ok, not exactly breaking news, but I’ve just discovered the Zend_Form_Decorator_ViewScript class, the Zend DevZone article covering it, well, it helped me find it, but the examples are pretty useless if you want it to actually work! (No surprises there then, sorry DevZone.)

So to that end I thought I’d blog.

Zend Form Decorators can be hard enough to twist your brain around at the best of times. But the ViewScript Decorator makes forms easy again. You can apply the ViewScript decorator to any Zend_Form element, though I’d have thought that using it on the Form itself is all you’d really need.

File: ./application/forms/Example.php

<?php
/**
 * ViewScript Decorator example form
 */
class Form_Example extends Zend_Form
{
    /**
     * Initialise
     *
     * Set up the form, that's down to you,
     * set the ViewScript decorator for the form
     * @access public
     */
    public function init()
    {
        /* ... Initialise my form ... */
        $this->setDecorators( array(
            array( 'ViewScript', array( 'viewScript' => 'forms/example.phtml' ) )
        ) );
    }
}
?>

Remember that the path forms/example.phtml is relative to the configured view script directories. So in my app these are ./application/views/scripts/:./application/modules/default/views/scripts/:./application/modules/admin/views/scripts/.

Inside the view script, the form element you’re decorating is assigned to the element view property. So inside the view script ( $this->element instanceof Zend_Form ) === true.

That just leaves you with the view script itself now. Well, they’re just like any other view script except you only have $this->element property, your view helpers remain available to you as always.

File: ./application/views/scripts/forms/example.phtml

<form id="exampleForm" action="<?php echo $this->element->getAction() ?>" method="<?php echo $this->element->getMethod() ?>">
    <h2>Your Details</h2>
    <?php echo $this->element->email ?>
    <?php echo $this->element->dob ?>
    <h2>Account Details</h2>
    <?php echo $this->element->username ?>
    <?php echo $this->element->password ?>
    <?php echo $this->element->passwordconfirm ?>
    <h2>Terms & Conditions</h2>
    <p>Read our <a href="<?php echo $this->url( array(), 'terms_and_conditions' ) ?>">Terms & Conditions</a> before signing up.</p>
    <?php echo $this->element->termsandconditions ?>
    <?php echo $this->element->submit ?>
    <p class="small">NOTE: This is where you may want reassure your visitors that you're to be trusted.</p>
</form>

Now that’s a pretty simple example of how you’d use the ViewScript decorator. You may be able to achieve that markup using only the other decorators. I don’t know, and I can’t really be arsed enough to find out. I plan to just keep it simple and have a view script for each form that needs it, and use other decorators for the individual form elements.

It is now down to you to build the <form/> element, the individual form elements are still automatically generated though so you still get all those fancy features that made you want Zend_Form in the first place. Simply echo them out.