prostynick
prostynick

Reputation: 6219

How to use existing decorators in Zend Framework?

Following code:

$this->addElement('text', 'email', array(
    'label' => 'Your email address:',
));

$this->addElement('submit', 'submit', array(
    'label' => 'Sign Guestbook',
));

produces following HTML:

<form enctype="application/x-www-form-urlencoded" action="" method="post">
    <dl class="zend_form">
        <dt id="email-label">
            <label for="email" class="optional">Your email address:</label>
        </dt>
        <dd id="email-element">
            <input type="text" name="email" id="email" value="" />
        </dd>

        <dt id="submit-label">
            &#160;
        </dt>
        <dd id="submit-element">
            <input type="submit" name="submit" id="submit" value="Sign Guestbook" />
        </dd>
    </dl>
</form>

I know, I can write my own decorators, but I would like to know, how to use existing decorators, to create following HTML:

<form enctype="application/x-www-form-urlencoded" action="" method="post">
    <div>
        <label for="email" class="optional">Your email address:</label>
        <input type="text" name="email" id="email" value="" class="my_class" />
    </div>

    <div>
        <input type="submit" name="submit" id="submit" value="Sign Guestbook" class="my_class" />
    </div>
</form>

No <dl/>, <dt/>, <dd/>, added class attribute.

For example, I know, how to remove surrounding <dl/> tag:

$this->addDecorator('FormElements')
     ->addDecorator('Form');

Are other changes possible without writing custom decorators?

Upvotes: 0

Views: 405

Answers (2)

zerkms
zerkms

Reputation: 254916

This (append this array to your parameters array, which now consists of only one label option) should help with your email field:

'class' => 'my_class',
'decorators' => array(
    'ViewHelper',
    'Errors',
    'Label',
    array('HtmlTag', array('tag' => 'div'))
)

And the same without Label - for submit.

Upvotes: 1

Sebastian Hoitz
Sebastian Hoitz

Reputation: 9373

You can use the HtmlTag decorator and pass which tag to use as a parameter.

All available decorators are listed here: http://framework.zend.com/manual/en/zend.form.standardDecorators.html

Upvotes: 1

Related Questions