Zendcasts Forum

A community of developers who work with the Zend Framework and other enterprise PHP technologies

You are not logged in.

#1 2010-02-22 17:06:46

Jon Lebensold
Administrator
Registered: 2009-06-27
Posts: 279

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.

Offline

#2 2010-02-23 17:46:21

iznogood
Member
From: Montreal
Registered: 2009-12-17
Posts: 20
Website

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!

Offline

#3 2010-02-23 22:27:37

rpsimao
New member
From: Lisbon, Portugal
Registered: 2009-12-13
Posts: 8

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

Offline

#4 2010-02-24 14:07:04

kobescoresagain
New member
Registered: 2010-02-24
Posts: 2

Re: ZC47 – Zend_Form Decorators Explained

Jon,

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

Offline

#5 2010-02-24 20:35:50

ulduz114
New member
Registered: 2010-02-24
Posts: 4

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)

Offline

#6 2010-02-25 14:32:28

Jon Lebensold
Administrator
Registered: 2009-06-27
Posts: 279

Re: ZC47 – Zend_Form Decorators Explained

just include the "FormErrors" decorator! smile

Offline

#7 2010-03-09 21:03:33

rafaelcavalcanti
New member
From: Brazil
Registered: 2010-03-09
Posts: 5

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

Offline

#8 2010-03-14 13:38:42

rafaelcavalcanti
New member
From: Brazil
Registered: 2010-03-09
Posts: 5

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.

Offline

#9 2010-03-22 12:35:18

zormi
New member
Registered: 2010-03-22
Posts: 1

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.

Offline

#10 2010-06-04 10:40:03

alessio
Member
Registered: 2009-08-07
Posts: 27

Re: ZC47 – Zend_Form Decorators Explained

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

Offline

#11 2010-06-29 04:54:26

SocialEvil
New member
Registered: 2010-05-26
Posts: 6

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)

Offline

#12 2010-09-14 20:31:54

__fabrice
New member
Registered: 2010-09-14
Posts: 5

Re: ZC47 – Zend_Form Decorators Explained

Hi there,

I've a question about thr "FormErrors" décorator, so :

        $this->setDecorators(array(
            array('FormErrors', array()),           
            'FormElements',
            array('HtmlTag', array('tag' => 'table', 'class'=> 'table_edit_news')),
            'Form'
        ));

This code generate the html, accordding the ZF website  :

<ul class="form-errors>
  <li><b>[element label or name]</b>
  <ul>
          <li>[error message]</li>
          <li>[error message]</li>
  </ul>
  </li>
</ul>

How can I remove the [element label or name] line : <b>[element label or name]</b>
I  know I can modify "markupElementLabelStart " and "markupElementLabelEnd " but not remove. In fact, I want only the list of errors, in the "FormErrors" decorator.

Any idea ?

Regards,
Fabrice

Last edited by __fabrice (2010-09-14 20:35:59)

Offline

#13 2011-03-24 00:58:25

jeff shipman
New member
Registered: 2011-01-14
Posts: 2

Re: ZC47 – Zend_Form Decorators Explained

Hello,

I am trying to create a printable representations of a completed form.
So, instead of a input field, I have added a form element called Note

class SF_Form_Element_Note extends Zend_Form_Element_Xhtml
{

public $helper = ‘formNote’;
}

My problem is that I am trying to place a span tag around the data text

my decorator for this field is as follows:

public function printDecorators ($id)
{

return array(
array(‘ViewHelper’, array(array(‘data’ => ‘HtmlTag’), array(‘tag’ =>’span’)),
array(array(‘data2′ => ‘HtmlTag’), array(‘tag’ =>’span’, ‘id’ => $id))),
array(‘Label’, array(‘tag’ =>’span’)),
array(‘Errors’),
array(‘HtmlTag’)
);

I get

<div>
</div><span id="printtab-first_name-label"><label for="printtab-first_name" class="optional">First Name</label></span>
Jeffery</div>
<div><span id="printtab-last_name-label"><label for="printtab-last_name" class="optional">Last Name</label></span>
Shipman</div>
</div>

what I would like is

<div>
</div><span id="printtab-first_name-label"><label for="printtab-first_name" class="optional">First Name</label></span>
<span id="first">Jeffery</span></div>
<div><span id="printtab-last_name-label"><label for="printtab-last_name" class="optional">Last Name</label></span>
<span id="last">Shipman</span></div>
</div>

Offline

Board footer

Powered by FluxBB