Custom Form Layout with Zend Form Decorator ViewScript

Zend Form is one of the most powerful tools of Zend Framework. One of the most common misconceptions is that Zend Form cannot be rendered in it’s own individual parts. The way to fully customise a form is by using the using the Zend Form ViewScript Decorator.

Form File

forms/MyLoginForm.php

class Form_MyLoginForm extends Zend_Form
{
    public function init()
    {
        $this->setDisableLoadDefaultDecorators(true);
 
        $this->setDecorators(array(
            array('ViewScript', array('viewScript' => 'form/_form.phtml')),
            'Form'
        ));
 
        $this->setMethod('post');
        $this->setAction('');
 
        $this->addElement('text', 'username', array(
            'decorators' => array(
                'ViewHelper'
            ),
        ));
 
        $this->addElement('password', 'password', array(
            'decorators' => array(
                'ViewHelper'
            ),
        ));
 
        $this->addElement('submit', 'submit-button', array(
            'decorators' => array(
                'ViewHelper'
            ),
            'label' => 'Submit'
        ));
    }
}

Form View Script

application/views/scripts/form/_form.phtml

<div style="padding: 8px;">
    <table style="width: 100%;">
        <tbody>
            <tr>
                <td>Username:</td>
                <td><?=$this->getElement('username'); ?></td>
            </tr>
            <tr>
                <td>Password:</td>
                <td><?=$this->getElement('password'); ?></td>
            </tr>
        </tbody>
    </table>
    <?=$this->element->getElement('submit-button'); ?>
</div>

Controller

application/controllers/IndexController.php

class IndexController extends Zend_Controller_Action
{
    public function indexAction()
    {
        $this->view->loginForm = new Form_MyLoginForm();
    }
}

Controller View Script

application/views/scripts/index/index.phtml

<h2>Login Form</h2>
<?php echo $this->loginForm; ?>

Permalink: http://www.websitefactors.co.uk/zend-framework/2011/05/custom-form-layout-with-zend-form-decorator-viewscript/

This entry was posted in Zend Framework. Bookmark the permalink.

12 Responses to Custom Form Layout with Zend Form Decorator ViewScript

  1. joey says:

    Thanks for this post =D

    However, I was not able to display the form.
    I only got the “Login Form” in the index.phtml.

    It seems like it could not find the path of the _form.phtml because when I tried to comment out the line setting the ViewScript path, it displayed the text box.

    I even tried to force the path like so but it did not give me anything.

    $decorPath = APPLICATION_PATH. ‘/form/_form.phtml’;
    $this->setDecorators(array(
    array(‘ViewScript’, array(‘viewScript’ => $decorPath )),
    ‘Form’
    ));

    Please help.

    • Henry Hayes says:

      Hi Joey,

      That is unlikely to work because the decorator is using a path prefix of your standard views/scripts path. In a standard installation, the path prefix would be: application/views/scripts/.

      So, your file should go here: application/views/scripts/form/_form.phtml

      I’m not sure what path it would end up trying to load; a speculation without testing would probably be that it would be trying to load this: application/views/scripts/[application-path]/form/_form.phtml. As you can see, this path would not exist and would never work.

  2. joey says:

    Your right Sir Henry. When I put my viewscript decorator to that path, it worked. =D

    Sorry I am just new to zend and I am not really aware of this.

    By the way, how can I change the path for my decorator viewscript so that it would not use its default path prefix?

  3. Lucas says:

    Thanks for this post – I finally was able to get my custom forms working.

    However there is one error in your code above. In:
    application/views/scripts/form/_form.phtml

    $this->getElement(‘username’);

    should be

    $this->element->getElement(‘username’);

    Same on the password field.

    • Henry Hayes says:

      My code isn’t wrong, it depends on the use case of the view decorator.

      I.e. if you are using it for a form or for a form field group (fieldset) then you should use your example.

      Also, you can iterate through the elements using:

      foreach ($this->element as $_element) {
      // Here, you can set styling around your elements etc.
      $elementToUse = $_element;
      }

      It all depends on the use case of the ViewHelper decorator.

      • I got errors when I left off the element-> part. With that addition, it works. Lucas was right in my case. Could this be because of some configuration issue? In any case,it works now. Thanks!

  4. Nao Yoshino says:

    Excellent breakdown, thank you very much!

    I’m having difficulty getting the actual form elements to display, however. For some reason, the following:

    getElement('property_type')?>

    is being rendered in the view as follows:

    < ?php=$this->getElement('property_type')?>

    Have turned Google upside-down but couldn’t find any references to this. It’s like it’s being escaped but I have no idea why.

    Any idea?

    Thanks,

    Nao

  5. Ian Lewis says:

    Thanks Henry,

    Apparently my comment got mangled.. I am no fan of short-tags in PHP. I positively discourage them and my developers’ IDEs are configured to prevent the use of them. Looks like my original comment was edited – possibly because the php opening tag was stripped.

    Use this format:

    getElement('property_type') ?>

  6. Ian Lewis says:

    OK.. mu comments keep getting mangled..

    <?php echo

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre lang="" line="" escaped="">

Notify me of followup comments via e-mail. You can also subscribe without commenting.