R – How to change the layout of a form with Zend_Form decorators

decoratorzend-decoratorszend-formzend-framework

I'm totally confused about how decorators work. This is the html structure that I'm trying to achieve:

<form id="" action="" method="post">

    <fieldset><legend>Contact form</legend>
        <p>
            <label for="name">Name</label>
            <input type="text" name="name" id="name" size="30" />
        </p>
        <p>
            <label for="email">Email</label>
            <input type="text" name="email" id="email" size="30" />
        </p>
        <p>
            <label for="web">Website</label>
            <input type="text" name="web" id="web" size="30" />
        </p>                                                                                    
        <p>
            <label for="message">Message</label>
            <textarea name="message" id="message" cols="30" rows="10"></textarea>
        </p>                    

        <p class="submit"><button type="submit">Send</button></p>       

    </fieldset>                 

</form> 

How do I get rid of the definition list format and use a paragraph tag based layout instead?

Update: Is there a way for me to apply this style to all the forms I have created? instead of having to apply the decorators to each and every form?

Best Answer

If you want to change the elements of your form you have to reset the Decorators of your Form and it's elements.

Example of enclosing a field in a p-tag

class Default_Form_Contact extends Zend_Form 
{
    public function init()
    {
        $name = new Zend_Form_Element_Text('name');
        $name->setLabel('Name:')
             ->setDecorators(
                array(
                  array('ViewHelper', array('helper' => 'formText')),
                  'Errors',
                  array('Description', array('tag' => 'p', 'class' => 'description')),
                  array('HtmlTag', array('tag' => 'p', 'id'  => 'name-element')),
                  array('Label', array('class' => 'label')),
                )
              ); 
        $this->addElement($name);
    }    
}

Which decorators you really need you have to consider yourself. For the form decorators you can do in the init()

$this->setDecorators(array(some decorators));
Related Topic