views:

291

answers:

2

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?

A: 

After a while, I just gave up and used just the ViewRenderer decorator. Here is a good post explaining how to do that (http://weierophinney.net/matthew/archives/215-Rendering-Zend_Form-decorators-individually.html). The rest of that series is good as well if you really want to know how to use decorators.

With the ViewRenderer decorator, you're basically rendering your form against a template (not unlike MVC itself). This way gives you the ultimate control over everything, but of course what you gain in flexibility, you lose in RAD, and you make up in complexity.

blockhead
A: 

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));
Peter Smit
is there a way to apply this globally?
Andrew
You can subclass all Zend_Element classes and overload loadDefaultDecorators.
Peter Smit