Creating Custom Zend_Form Decorators

February 28th, 2010

This little video tutorial should set you up for building your own custom Zend_Form decorators in 15 minutes. I’ll show you how you can make the necessary class and have it easily added to your existing Zend_Form_Decorator configuration.

Grab a copy of the project or browse the repository.

In other news, ServerGrove is giving Zendcast viewers a coupon! ServerGrove specializes in Zend Framework hosting and they’ve offered a 10% rebate on hosting with coupon code “zc”. If you’re looking for a host, be sure to check them out (referral). They’ve also added an additional coupon for “Mini Hosting” plans, get $2 off by using code “zcmini”.
 

6FEXTBMDW93N

discuss video in the forum

16 Responses to “Creating Custom Zend_Form Decorators”

  1. Exactly what I needed! Thanks a lot!

  2. Sweet talk, this one and the previous on decorators have been insanely useful.

    One suggestion that would seem appropriate for the decorator you made in this talk, is to use:
    $Form->setDescription($text);
    and then $Form->getDescription();

    Obviously we could set that as the text option when we add our decorator, however are there any options for a more automatic approach such as getting the form from within the decorator? At first glances it doesn’t seem like that’s possible.

    Again great talk, really helpful.

  3. jkulak says:

    Great! I was just digging in to this thing.

  4. jon says:

    hey Benjamin,

    you’re absolutely correct about setDescription being an option, however simply using the method available wouldn’t have helped illustrate how the decorators are constructed.

  5. J Dichev says:

    I was looking for video like this one for a while. Thanks :)
    By the way, I had to spend some time last year in order to find out how could prefix paths be set from ini config file. Here’s what worked for me:

    post.edit.elementPrefixPath.validate._Validate.path = “/Validate”
    post.edit.elementPrefixPath.validate._Validate.prefix = “_Validate”

    // ‘post’ usually being a controller name
    // ‘edit’ usually being an action name

  6. Master B. says:

    Yes!
    With such examples it makes more easier and comfortable to work with Zend Framework!
    I opend my eyes to make my code better in no time! Thank’s!

  7. strick says:

    The only thing I don’t like about decorators for something like that is that when the text needs to be changed, I (the programmer) have to be bugged to change it. Note, I do like decorators for their re-usability, but lately I’ve just been dumping all the individual form elements out to the page (as you mentioned in the beginning) so that I never have to worry about changing little things like text, e.g.

    Give us your name
    form->getFirstName(‘First Name:’)?>

  8. jon says:

    @strick: I definitely agree with you, however there are other implications to this. Obviously passing strings is a very simple example, however nothing is stopping you from building a view helper/script and loading it via the decorator. Being able to pass parameters from the form into the decorator also lets you easily adjust the forms’ markup via your own custom implementation. Passing text is only the tip of the iceberg!

  9. Ishi says:

    setting prefix paths from config file

    // using an INI config file:
    form.elementPrefixPath.zc.prefix = Zc_Form_Decorator”
    form.elementPrefixPath.zc.path = “Zc/Form/Decorator”
    form.elementPrefixPath.zc.type = “decorator”

    // Or using an XML config file:

    Zc_Form_Decorator
    Zc/Form/Decorator
    decorator

  10. Ishi says:

    sorry
    <form>
    <elementPrefixPath>
    <my>
    <prefix>My_Form_Decorator</prefix>
    <path>My/Form/Decorator</path>
    <type>decorator</type>
    </my>
    </elementPrefixPath>
    </form>

  11. jon says:

    Thanks Ishi! I’ll try it out this week :)

  12. azz0r says:

    How would I set the html to be a ul/li set up through the decorator class?

    Render doesnt seem to have access.

  13. Arne Riemann says:

    Very good Tutorial. Hope you create some more ! Thanks a lot!

  14. selecto says:

    Great presentation!

  15. Milan Borah says:

    Heya¡­my very first comment on your site. ,I have been reading your blog for a while and thought I would completely pop in and drop a friendly note. . It is great stuff indeed. I also wanted to ask..is there a way to subscribe to your site via email?

Leave a Reply

Desktop RSS feed iPhone + iPod