Zend Framework

Zend Form Decorators

Posted by Faheem Abbas on October 23, 2008

14 Responses to “Zend Form Decorators”

  1. Ahsan Shahzad said

    Great Article!
    thanks for it, i really found it helpful.

    I was in search for some practical example and fortunately u provided here, thanks again :)

  2. Very nice article, but I had a problem implementing it.

    Hidden fields are still in definition list, If I add decorators to hidden fields, my CSS will be applied to those TABLE ROWS too.

    Any solution?

  3. Faheem Abbas said

    your hidden filed by default appear like

    dt> /dt>
    dd>
    input type=”hidden” name=”id” value=”" id=”id”>/dd>

    and you want it like this, I think
    input type=”hidden” name=”id” value=”" id=”id”>
    if you want this. then write

    $id->setDecorators(array(
    'viewHelper',
    'Errors',
    array('HtmlTag',array('tag'=>'b'))
    ));

    I am enclosing it in “b” tag so it will appear in form but not in row. you can even place it in “span” tag.
    The code below should also work, however In case of hidden fields it is not working as i have checked it here.

    $id->removeDecorator(‘DtDdWrapper’);

  4. Simply Awesome!
    It really helped me alot, keep it up!

  5. Hi,
    May be there are more than one solutions to accomplish this task, so for that I got another good piece of code which I wanted to share with all of you guys, here I’m putting it:

    class My_Form_Registration extends Zend_Form
    {
    public $elementDecorators = array(
    ‘ViewHelper’,
    ‘Errors’,
    array(array(‘data’ => ‘HtmlTag’), array(‘tag’ => ‘td’, ‘class’ => ‘element’)),
    array(‘Label’, array(‘tag’ => ‘td’),
    array(array(‘row’ => ‘HtmlTag’), array(‘tag’ => ‘tr’)),
    );

    public $buttonDecorators = array(
    ‘ViewHelper’,
    array(array(‘data’ => ‘HtmlTag’), array(‘tag’ => ‘td’, ‘class’ => ‘element’)),
    array(array(‘label’ => ‘HtmlTag’), array(‘tag’ => ‘td’, ‘placement’ => ‘prepend’)),
    array(array(‘row’ => ‘HtmlTag’), array(‘tag’ => ‘tr’)),
    );

    public function init()
    {
    $this->addElement(‘text’, ‘username’, array(
    ‘decorators’ => $this->elementDecorators,
    ‘label => ‘Username:’,
    );
    $this->addElement(‘text’, ‘firstname’, array(
    ‘decorators’ => $this->elementDecorators,
    ‘label => ‘First Name:’,
    );
    $this->addElement(‘text’, ‘lastname’, array(
    ‘decorators’ => $this->elementDecorators,
    ‘label => ‘Last Name:’,
    );
    $this->addElement(’submit’, ’save’, array(
    ‘decorators’ => $this->buttonDecorators,
    ‘label => ‘Save’,
    );
    }

    public function loadDefaultDecorators()
    {
    $this->setDecorators(array(
    ‘FormElements’,
    array(‘HtmlTag’, array(‘tag’ => ‘table’)),
    ‘Form’,
    ));
    }
    }

    Enjoy!

  6. Faheem Abbas said

    yes i have applied same technique in my application. however I wrote this post to show how we can use decorators. another post http://zendguru.wordpress.com/2008/11/11/applying-decorators-to-all-elements-of-form-elements-at-once/ is good place to apply decorators to all form elements at once.

  7. Developer said

    Great Work :)

  8. [...] Excellent References: http://framework.zend.com/manual/en/zend.form.standardDecorators.html http://devzone.zend.com/article/3450-Decorators-with-Zend_Form http://zendguru.wordpress.com/2008/10/23/zend-form-decorators/ [...]

  9. [...] http://devzone.zend.com/article/3450-Decorators-with-Zend_Form http://zendguru.wordpress.com/2008/10/23/zend-form-decorators/ Possibly related posts: (automatically generated)Zend Form [...]

  10. zend dev said

    Great Article
    very helpful indeed

  11. zend dev said

    hi

    when i use decorators,
    it places legend in a separate row and all elements that i had grouped i the legend out of the legend in separate rows

    why’s this so?

  12. If you want to group form elements, read this.
    http://zendguru.wordpress.com/2008/11/03/zend-form-display-groups-and-decorators-with-example/

  13. Mastertikaf said

    Hi !
    I’m new in the Zend World and this article is very helpfull !

    Althougt you said that “We are skipping “ViewHelper”, “Description” and “Errors” for now.” and that I want to now it’s :

    Why do we need to put them in the decorators setting declarations ? I don’t find the answer.

    Thanks !
    Great job.

  14. “viewHelper”: for displaying our form elements in view template.
    “Description”: for appending for pre-pending description next to form element.
    “Error”: for displaying error messages next to each element, if any.
    This is short description bro.

Leave a Reply

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <pre> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>