List Info

Thread: Zend_Form global decorators overriding per element decorators when using array of optio




Zend_Form global decorators overriding per element decorators when using array of optio
user name
2008-05-07 08:45:04
Hello.

I've been playing with Zend_Form today, specifically with
the text
decorators, however it's not behaving quite how I expect.

I've extended the Zend_Form class, and I create the form
using an array of
options, like so:
class forms_MediaOutletForm extends Zend_Form
{

        public function __construct($options = null) {

                $loader = Zend_Registry::get('loader');

                $loader->load('FormUtils');

                parent::__construct(array(
                        'action' =>
'/mediaoutlet/process',
                        'method' => 'post',
                        'decorators' => array(
                                'FormElements',
                                array('HtmlTag', array('tag'
=> 'p')),
                                ),
                        'elementDecorators' => array(
                                       
array('ViewHelper'),
                                        array('Errors'),
                                        array('Label',
array(
                                               
'requiredSuffix' => ' *',
                                                'class'
=> 'leftalign',
                                        )),
                                        array('HtmlTag',
array('tag' =>
'div', 'class' => 'field')),
                                ),
                        'elements' => array(
                                'MediaOutletId' =>
array('hidden', array(
                                        'validators' =>
array(
                                                'digits',
                                        ),
                                        'readonly' =>
true,
                                )),
                                'Name' => array('text',
array(
                                        'validators' =>
array(
                                               
array('stringLength', false,
array(3,70)),
                                        ),
                                        'filters' =>
array('StringTrim'),
                                        'required' =>
true,
                                        'label' =>
'Name',
                                )),
                                'CompanyID' =>
array('select', array(
                                        'validators' =>
array(
                                                'notEmpty',
                                        ),
                                        'required' =>
true,
                                        'MultiOptions'
=>
Companies::getIDName(array("Publisher")),
                                        'label' =>
'Publisher',
                                        'decorators' => 
array(
                                               
array('ViewHelper'),
                                               
array('Errors'),
                                               
array('Label', array(
                                                       
'requiredSuffix' =>
' XXX',
                                                       
'class' =>
'leftalign',
                                                )),
                                               
array('DivTag' => 'HtmlTag',
array('tag' => 'div', 'id' => 'company_div' )),
                                        )
                                )),
),
                ));
        }
}

What's happening is that the specific element decorate is
being overridden
due to the setOptions method in Zend/Form.php doing this:
$this->setElementDecorators($elementDecorators);

after all the elements are set.  This is causing my specific
element
decorators to be overridden.

Does anyone have any ideas on how I can do what I'm wanting
while staying
with this method of form configuration?

Cheers,
Jonathan.
-- 
View this message in context: http://www.nabble.com/Z
end_Form-global-decorators-overriding-per-element-decorators
-when-using-array-of-options-to-construct-tp17105287p1710528
7.html
Sent from the Zend Framework mailing list archive at
Nabble.com.


Re: Zend_Form global decorators overriding per element decorators when using array of o
user name
2008-05-07 11:31:09
-- Jonathan Tullett <jonathandownagain.com> wrote
(on Wednesday, 07 May 2008, 06:45 AM -0700):
> 
> Hello.
> 
> I've been playing with Zend_Form today, specifically
with the text
> decorators, however it's not behaving quite how I
expect.
> 
> I've extended the Zend_Form class, and I create the
form using an array of
> options, like so:

<snip>

> What's happening is that the specific element decorate
is being overridden
> due to the setOptions method in Zend/Form.php doing
this:
> $this->setElementDecorators($elementDecorators);
> 
> after all the elements are set.  This is causing my
specific element
> decorators to be overridden.
> 
> Does anyone have any ideas on how I can do what I'm
wanting while staying
> with this method of form configuration?

Zend_Form::setOptions() delays the setElementDecorators()
call to after
all other configuration, so that any elements set using
configuration
will then be affected by it. There is no way to override
it.

An easier approach is to define an array of default
decorators you want
to use as an class property:

    public $defaultElementDecorators = array(...);

Then, omit your elementDecorators declaration, and declare
your
decorators for each element by referencing this property:

    'foo' => array(
        'decorators' =>
$this->defaultElementDecorators
    ),

For elements where you want custom decorators, you do as you
were doing
previously.

-- 
Matthew Weier O'Phinney
Software Architect       | matthewzend.com
Zend - The PHP Company   | http://www.zend.com/

Re: Zend_Form global decorators overriding per element decorators when using array of o
user name
2008-05-07 11:58:04

> Zend_Form::setOptions() delays the
setElementDecorators() call to after
> all other configuration, so that any elements set using
configuration
> will then be affected by it. There is no way to
override it.
Right, that's what I suspected.

> An easier approach is to define an array of default
decorators you want
> to use as an class property:

>    public $defaultElementDecorators = array(...);

> Then, omit your elementDecorators declaration, and
declare your
> decorators for each element by referencing this
property:
>    'foo' => array(
>       'decorators' =>
$this->defaultElementDecorators
>    ),
I had considered this but I wondered if there was a better
way to manage it
without duplicating configuration 
on each element.

If that's the most straight forward way to do it then great,
that's how it
shall be done 

Thanks for getting back to me so quickly.
Jonathan.
-- 
View this message in context: http://www.nabble.com/Z
end_Form-global-decorators-overriding-per-element-decorators
-when-using-array-of-options-to-construct-tp17105287p1710966
8.html
Sent from the Zend Framework mailing list archive at
Nabble.com.


[1-3]

about | contact  Other archives ( Real Estate discussion Medical topics )