views:

216

answers:

2

I would like to get rid of the definition list format of my Zend_Form. This is the layout that I'm going for:

<form>
    <p>
        <label for="email" class="required">Your email address:</label>
        <input type="text" name="email" id="email" value="">
    </p>
    <p>
        <input type="submit" name="submit" id="submit" value="Subscribe">
    </p>
    <input type="hidden" name="active" value="true" id="active">
    <input type="hidden" name="signupDate" value="" id="signupDate">
</form>

What do I need to do to my form in order to get this layout?

class Default_Form_Subscribe extends Zend_Form
{
    public function init()
    {
        $this->setMethod('post');

        $this->addElement('text', 'email', array(
            'label'      => 'Email address:',
            'required'   => true,
            'filters'    => array('StringTrim'),
            'validators' => array('EmailAddress')
        ));

        $this->addElement('submit', 'submit', array(
            'label'    => 'Subscribe',
            'ignore'   => true
        ));

        $this->addElement('hidden', 'active', array(
            'value'=>'true'
        ));
        $this->addElement('hidden', 'signupDate', array(
            'value' => Zend_Date::now()->toString('YYYY-MM-dd')
        ));
    }
}
+1  A: 

You have to customize your Zend_Form elements's decorators. Check this tutorial.

In your case it will be something similar to this:

$form->setElementDecorators(array(
    'ViewHelper',
    'Errors',
    array('Label', array('tag' => 'label', 'placement' => 'prepend'),
    array(array('data' => 'HtmlTag'), array('tag' => 'p')),
));

That sets the decorators for all the form elements. You can as well configure individual elements (like your hidden-s & buttons).

It's possible also to form display groups, and decorate them individually.

Ivan Krechetov
+1  A: 

Ah, beat me to it... I went with the approach of creating a custom definition that can be applied to specific elements. Also had to reset the decorators on the form itself to remove the default 'dl' wrapper, seems to do exactly what you need:

class Default_Form_Subscribe extends Zend_Form
{
    public function init()
    {
        $this->setMethod('post');

        // reset form decorators to remove the 'dl' wrapper
        $this->setDecorators(array('FormElements','Form'));

        // custom decorator definition for form elements
        $customElementDecorators = array(
            'ViewHelper',
            'Errors',
            array(
                'Description',
                array('tag' => 'p','class' => 'description')
            ),
            array(
                'Label',
                array('separator' => ' ')
            ),
            array(
                array('data' => 'HtmlTag'),
                array('tag' => 'p')
            )
        );

        $this->addElement('text', 'email', array(
            'label'      => 'Email address:',
            'required'   => true,
            'filters'    => array('StringTrim'),
            'validators' => array('EmailAddress'),
            'decorators' => $customElementDecorators
        ));

        $this->addElement('submit', 'submit', array(
            'label'    => 'Subscribe',
            'ignore'   => true,
            'decorators' => $customElementDecorators
        ));

        $this->addElement('hidden', 'active', array(
            'value'=>'true',
            'decorators' => array('ViewHelper')
        ));
        $this->addElement('hidden', 'signupDate', array(
            'value' => Zend_Date::now()->toString('YYYY-MM-dd'),
            'decorators' => array('ViewHelper')
        ));
    }
}
Will Prescott