-- Simon Mundy <[EMAIL PROTECTED]> wrote
(on Saturday, 26 January 2008, 02:57 PM +1100):
> Would it be possible to identity form elements passed to a display group 
> either as strings or as the elements themselves? I'm trying to do the 
> following:-
>
> (Where each variable is an already-added instance of Zend_Form_Element e.g. 
> $name_title = new Zend_Form_Element('text') )
>
>         $groupPersonal = $this->addDisplayGroup(array(
>             $name_title,
>             $name_first,
>             $name_last,
>             $name_preferred,
>             $birthday,
>             $position,
>             $personal_address_1,
>             $personal_address_2,
>             $personal_telephone,
>             $personal_facsimile,
>             $personal_mobile,
>             $personal_email,
>         ), 'one');
>         $groupPersonal->setLegend('Personal details');
>
> Two problems:-
>
> a) It doesn't recognise these as valid elements (it only recognises string 
> names)

I'm not sure that will work. Reasons are below.

> b) The 'addDisplayGroup' returns an instance of the form, not the 
> newly-added group. Wouldn't it be more consistent to return the group 
> object? Otherwise I'd want to create an instance of Zend_Form_DisplayGroup 
> and then add that to the form, but I can't see how.

The reason it returns the form object is so that you can method chain
and perform a number of add*() options in a row:

    $form->addElements(array(
            'username' => 'text',
            'password' => 'password',
            'fullname' => 'text',
            'email'    => 'text',
            'address'  => 'text',
            'postal'   => 'text',
            'submit'   => 'submit',
            'cancel'   => 'submit'
        ))
        ->addDisplayGroup(array('username', 'password'), 'login')
        ->addDisplayGroup(array('fullname', 'email', 'address', 'postal'), 
'demographics')
        ->addDisplayGroup(array('submit', 'cancel'), 'actions');

After you've added a display group, you can then retrieve it by name:

    $actions = $form->actions;
    $actions = $form->getDisplayGroup('actions');

There are several reasons that addDisplayGroup() is the preferred method
for creating display groups:

    * allows it to inherit settings from the form, such as decorator
      paths
    * performs some logic in the form object to remove the elements in
      the display group from iteration
    * ensures that elements registered in the display group are also
      registered in the form (allowing access to them in the form)

I can probably overcome these issues by having the display group have
knowledge of its parent form, but will need to do some significant
refactoring to do so.

What is the use case for you? E.g., why do you want to instantiate
display groups separately and add concrete elements to them instead of
using addDisplayGroup and passing the element names? What problem does
it solve? (Just gathering information here for the decision making
process.)

-- 
Matthew Weier O'Phinney
PHP Developer            | [EMAIL PROTECTED]
Zend - The PHP Company   | http://www.zend.com/

Reply via email to