-- Nick Lo <[EMAIL PROTECTED]> wrote
(on Friday, 29 February 2008, 10:39 AM +1100):
> Bit of a shocker of a subject line but I'm a bit busy to think of a better
> one right now! The "cascading" is with reference to the way CSS works
> really .
>
> What I'm after is just some confirmation that the current general flow for
> the setting of default decorators with Zend_Form has to be something like
> this:
>
> - instantiate form
> - setup element 1
> - setup element 2
> - setup element 3
> - clear all current decorators
> - set my own element defaults
> - render
>
> ...or...
>
> - instantiate form
> - setup element 1
> - clear current decorators for element 1
> - set my own decorators for element 1
> - setup element 2
> - clear current decorators for element 2
> - set my own decorators for element 2
> - setup element 3
> - clear current decorators for element 3
> - set my own decorators for element 3
> - render
>
> ?
>
> The reason I ask is that I'd like to be able to work like so:
>
> - instantiate form
> - clear all current decorators
> - set my own element defaults
> - setup element 1
> - setup element 2 but tweak slightly from default
> - setup element 3
> - render
>
> With the affect working a bit like a subclass or the way CSS works whereby
> I clear and establish defaults before setting up my elements and then can
> alter each one as needed in a "cascading" way.
>
> This may already be possible but I wonder whether, judging by another
> comment on another post, the way I'm setting it up breaks it.
No. Currently when you call setElementDecorators(), it affects only the
currently existing decorators, not ones added after that method has been
called.
> I'm setting up each element by itself then adding it to the form like
> so...
>
> $email = new Zend_Form_Element_Text('email', array('label' => 'Email
> Address', 'size' => '30'));
> $email->addValidator('emailAddress');
> $this->addElement($email);
>
> ...and I wonder if using the factory approach would mean each element would
> inherit my previously set custom decoration:
>
> $this->addElement('text', 'email', array('label' => 'Email'));
>
> The point really is to cut down on what currently feels to be a lot of
> similar settings and the difficulty in having each element with just a
> slight variation here and there on my defaults.
>
> Hope that makes sense?
Yes and no.
The point of setElementDecorators() was simply for setting decorators en
masse. During the earlier stages of Zend_Form's development, this seemed
like an excellent solution to modifying all decorators for all elements
at once, as they all used the same defaults. However, now there are a
number of default elements that use a different set of decorators --
e.g., Submit, Reset, and Button, and some others -- so setting them en
masse is less of a reasonable option than it was before (depending on
the decorators you want to use). With that in mind, I understand your
request pretty well, and I can see this as a potential change in
behavior (i.e., allowing setElementDecorators to trigger for all
elements added after it has been called, as well as existing elements);
I'll think on it (the implementation would be non-trivial).
One thing you may want to try is setting the decorators you want to use
as an independent array. For instance:
$decorators = array(
'ViewHelper',
'Errors',
'Label'
array('HtmlTag', array('tag' => 'div', 'class' => 'element'))
);
You could then use this array when instantiating your elements or using
the factory:
$email = new Zend_Form_Element_Text('email', array(
'label' => 'Email Address',
'size' => '30',
'decorators' => $decorators,
));
// or
$form->addElement('text', 'email', array(
'label' => 'Email Address',
'size' => '30',
'decorators' => $decorators,
));
Then, instead of tweaking decorators *later*, after the element has
already been instantiated, simply pass in different decorators to the
elements that need them:
$save = new Zend_Form_Element_Submit('save', array(
'label' => 'Save!',
'decorators' => array(
'ViewHelper'
),
));
This will be more performant in the long run (no need to instantiate
decorators you won't be using when the object is first initialized), and
gives you similar benefits to what you're requesting.
--
Matthew Weier O'Phinney
PHP Developer | [EMAIL PROTECTED]
Zend - The PHP Company | http://www.zend.com/