1

Topic: ZC47 – Zend_Form Decorators Explained

One of the pain points for folks who are starting to work with the Zend Framework is the Decorating functionality found in the depths of Zend_Form. I've witnessed countless instances when a developer becomes excited by Zend_Form's easy-to-implement form validation and creation, only to become frustrated by countless hours of fighting with Zend_Form_Decorators. This video is a humble attempt on my part to walk through how Zend_Form Decorators work and how you can reason your way through a desire result. I couldn't have gotten my own head around this implementation of the decorator pattern without Matthew Weier O'Phinney's excellent posts and his original devzone article.

I'll show you a bit about how Zend_Form_Decorators are constructed and how to take the default zend_form layout and transform it into a table.

Grab a copy of the project or browse the repository.

2

Re: ZC47 – Zend_Form Decorators Explained

Wow what a nice video! I much prefer this style then the other ones. Personnaly I dont really need help coding little functionnality in my websites but a indepth look at a ZF element like form decorators is really great and saves me the hassle of having to discover it all by myself.
I'll try to implement this. As of now I use forms and ViewScript to display custom forms. It works well but it doubles the code I have to write.
Keep up the good work!

3

Re: ZC47 – Zend_Form Decorators Explained

Finally someone explain in good old english how the damn Decorators works.
The Zend_Form_Decorators are a mixed of the Decorator and Strategy Patterns, I believe, the concept is simple but it is hard to get.
Jon you're the man.
Thanks

4

Re: ZC47 – Zend_Form Decorators Explained

Jon,

Thanks for this informative video.  I cannot wait to see your video about custom decorators.

5

Re: ZC47 – Zend_Form Decorators Explained

Thanks for your useful videos.
i want to know how can i decorate error message?
and may you create a tutorial about implement WYSIWYG editors on zend framework?

Last edited by ulduz114 (2010-02-25 10:34:39)

6

Re: ZC47 – Zend_Form Decorators Explained

just include the "FormErrors" decorator! smile

7

Re: ZC47 – Zend_Form Decorators Explained

I faced some problems when I used the type 'file'.
At first showed the error:
"Warning: Exception caught by form: No file found ... decorator unable to render file element"
Simply place the decorator 'File'.

But from now shows another error:

Warning: Exception caught by form: Method getMaxFileSize does not exist

And I stopped here ever since. I tried several configurations:

        $ maxfilesize = new Zend_Form_Element_Hidden ( 'form element');
        $ maxfilesize-> setValue (204800);

   $ photo = new Zend_Form_Element_File ( 'photo');
        $ photo-> setLabel ( 'Photo')
                 -> setValidators (array (
                    array ( 'Count', false, 1),
                    array ( 'Size', false, 204800),
                    array ( 'Extension', false, 'jpg, gif, png'),
                    array ( 'NotEmpty', true)
                ))
               
                / / -> setMaxFileSize (20000)
                -> setRequired (true);

But still without success. Is there opportunity to help with this?
Thank you.
Att

8

Re: ZC47 – Zend_Form Decorators Explained

I believe I discovered the flaw but falls in further questions. According to this link:

http://framework.zend.com/wiki/display/ZFFAQ/Forms

It needs another decorator, following the example shown in zendcast 47 I can not put the two (File and viewhelp) decorators together. Does anyone have any suggestions?

Thank you.

9

Re: ZC47 – Zend_Form Decorators Explained

Jon Lebensold wrote:

just include the "FormErrors" decorator! smile

That's excactly what I tried to do.  'Errors' shouldn't be any different than 'Label'.  So this is what I did:

$this->setElementDecorators(array( 
       ‘ViewHelper ‘,
       array( 'Errors',
              array( 'tag' => 'td', 'placement' => 'APPEND' ),
        array(
            array( ‘data‘ => ‘HtmlTag‘), 
            array( ‘tag‘ => ‘td‘,‘class‘ => ‘element‘ ) 
       ),
       array( ‘Label‘, 
           array( ‘tag‘ => ‘td‘ )
       ),
       array(
            array(‘row‘ => ‘HtmlTag‘), 
            array( ‘tag‘ => ‘tr‘,‘class‘ => ‘row-element‘ )
       ) 
) );

In my head, it should be no different adding a td tag to the errors than to the label.  But my head does not always compute with Zend and in this case, what this does is adding an attribute to the ul element called td.

So where should I manipulate the Error decorator?  I want it to render itself the same ( ul -> li ), but I want it to have an additional td around the ul.

10

Re: ZC47 – Zend_Form Decorators Explained

if I wanted to insert errors without 'ul' and 'li'

11

Re: ZC47 – Zend_Form Decorators Explained

Ok. I did't get something.
in my source i have
<label...
<input ...

<label ...
<input...

so when when i decorate, i wrap and wrap and wrap..
but what if i want my labels to be on a row and my elements to be on another row so it will be like

<tr><td><label></label></td><td><label></label></td></tr>
<tr><td><input/></td><td><input/><td></tr>
how is this going to happen? I can't figure it out :S

Last edited by SocialEvil (2010-06-30 06:53:31)

Please allow me to introduce myself
I'm a man of wealth and taste
I've been around for a long, long year
Stole many a man's soul and faith