It seems that my approach is bugged and I'm not sure why.
The form renders properly, but when submitted only the
elements explicitly added with Zend_Form::addElement() appear when I
var_dump $form->getValues(). All of the elements added through
Zend_Form_DisplayGroup::addElement() do not show up in the values, but they
are rendered on the page.

Any ideas what might be going on?

--
Hector
Sent from Temecula, California, United States

On Thu, Jul 30, 2009 at 1:19 PM, Hector Virgen <djvir...@gmail.com> wrote:

> Thanks, Colin.
> After some tinkering I ended up doing something similar to your suggestion,
> but instead of recreating the display group, I used
> Zend_Form::createElement() instead of Zend_Form::addElement(). This allowed
> me to add the element to the form if the display group does not yet exist,
> or add it to the display group if it did (which, apparently, also adds the
> element to the form).
> Here's how it looked in the end:
>
> public function addStandard(Standard $standard)
> {
>     $name = 'standard_' . $standard->getId();
>     $element = $this->createElement('checkbox', $name);
>
>     $displayGroup = $this->getDisplayGroup('standards');
>     if (null === $displayGroup) {
>         $this->addElement($element);
>         $this->addDisplayGroup(array($name), 'standards');
>     } else {
>         $displayGroup->addElement($element);
>     }
> }
>
> It would be nice if the Zend_Form_DisplayGroup::addElement() was smart
> enough to know if the element already belongs to the form, but the
> workaround above isn't too bad.
>
> --
> Hector
>
>
>
> On Thu, Jul 30, 2009 at 1:03 PM, Colin Guthrie <gm...@colin.guthr.ie>wrote:
>
>> 'Twas brillig, and Hector Virgen at 30/07/09 18:56 did gyre and gimble:
>>
>>  Hello,
>>>
>>> I am having some trouble with Zend_Form and display groups (ZF 1.8.2). I
>>> want to add an existing form element to an existing display group within the
>>> same form, but when I call $displayGroup->addElement($element), the element
>>> is re-added to the form, causing it to be rendered twice (once in the
>>> display group, and again outside of the display group).
>>>
>>> Here is my code (trimmed for clarity):
>>>
>>> class MyForm extends Zend_Form
>>> {
>>>    public function init()
>>>    {
>>>        /* ... */
>>>    }
>>>
>>>    public function addStandard(Standard $standard)
>>>    {
>>>        $name = 'standard_' . $standard->getId();
>>>        $this->addElement('checkbox', $name);
>>>        $displayGroup = $this->getDisplayGroup('standards');
>>>        $displayGroup->addElement($this->getElement($name));
>>>    }
>>> }
>>>
>>> I have also tried just specifying the element's name, but that ends up in
>>> a fatal error:
>>>
>>>    Call to a member function getName() on a non-object in
>>>    /usr/share/pear/Zend/Form/DisplayGroup.php on line 433
>>>
>>>
>>> I also noticed that I cannot create a display group without adding
>>> elements to it:
>>>
>>> $this->addDisplayGroup(array(), 'standards'); // throws exception "No
>>> valid elements specified for display group"
>>>
>>> This makes it difficult to create empty display groups as containers for
>>> the elements to be added later.
>>>
>>> I can get around this by creating the display group automatically when
>>> the first "standard" is added, but I am stumped on how to add the next
>>> standard to the existing display group.
>>>
>>
>> I run up against the same problem.
>>
>> In the end I had to tear down and recreate the display group each time I
>> wanted to add an element to it. I totally meant to follow up for plumb
>> forgot - sorry :s
>>
>> Here is the code I use to get the desired end result. Ugly, but hey.
>>
>>
>> You can probably guess some element names but $el is the new element and
>> the $displayGroupLegend/Description vars are also filled in prior to this
>> code but can be overridden. $displayGroup is obviously the name of the
>> display group in question.
>>
>> HTHs
>>
>>    $this->_form->addElement($el);
>>    if (!empty($displayGroup))
>>    {
>>      $curr_elements = array();
>>      $dg = $this->_form->getDisplayGroup($displayGroup);
>>
>>      if (!is_null($dg))
>>      {
>>        // It seems that the only way to edit an existing displaygroup
>>        // is to remove and recreate it :s
>>
>>        // This would appear to be a bug in Zend Form.
>>        $displayGroupLegend = $dg->getLegend();
>>        $displayGroupDescription = $dg->getDescription();
>>        $cur_elements = array_keys($dg->getElements());
>>        $this->_form->removeDisplayGroup($displayGroup);
>>      }
>>
>>      $cur_elements[] = $el->getName();
>>      $this->_form->addDisplayGroup($cur_elements, $displayGroup);
>>      $dg = $this->_form->getDisplayGroup($displayGroup);
>>
>>      if (!empty($displayGroupLegend))
>>        $dg->setLegend($displayGroupLegend);
>>
>>      if (!empty($displayGroupDescription))
>>        $dg->setDescription($displayGroupDescription);
>>
>>    }
>>
>>
>> --
>>
>> Colin Guthrie
>> gmane(at)colin.guthr.ie
>> http://colin.guthr.ie/
>>
>> Day Job:
>>  Tribalogic Limited [http://www.tribalogic.net/]
>> Open Source:
>>  Mandriva Linux Contributor [http://www.mandriva.com/]
>>  PulseAudio Hacker [http://www.pulseaudio.org/]
>>  Trac Hacker [http://trac.edgewall.org/]
>>
>>
>

Reply via email to