Zend Form: Trying to add a Label Decorator to a DisplayGroup

5 messages Options
Embed this post
Permalink
jkendall

Zend Form: Trying to add a Label Decorator to a DisplayGroup

Reply Threaded More More options
Print post
Permalink
By default, a display group is wrapped in a Fieldset, allowing for a legend at the top of your display group.  I have a use case where I'd like to wrap the display group in li tags and add a label at the top:

<li>

    <label>Blah</label>

    DISPLAY GROUP

</li>

Attempting to add a Label element to a display group results in the following error: Call to undefined method Zend_Form_DisplayGroup::getLabel() in Zend/Form/Decorator/Label.php on line 242.

Subclassing DisplayGroup and adding a getter and setter for Label results in: Call to undefined method SJCRH_Form_DisplayGroup::isRequired() in Zend/Form/Decorator/Label.php on line 260.

Obviously, I'm missing something in regards to DisplayGroup and how it relates to Decorators, but I can't seem to figure it out.  Any help?
jkendall

Re: Zend Form: Trying to add a Label Decorator to a DisplayGroup

Reply Threaded More More options
Print post
Permalink
I should have paid more attention to the errors that I was getting.  They gave me the road map to extending DisplayGroup properly.  By extending Zend_Form_DisplayGroup and including the following methods, I was able to successfully apply a label to a DisplayGroup:

  public function setLabel($label) {
      return $this->setAttrib('label', (string) $label);
  }

  public function getLabel() {
      return $this->getAttrib('label');
  }
 
  public function setRequired($flag) {
      $this->_required = (bool) $flag;
      return $this;
  }

  public function isRequired() {
      return $this->_required;
  }

jkendall wrote:
By default, a display group is wrapped in a Fieldset, allowing for a legend at the top of your display group.  I have a use case where I'd like to wrap the display group in li tags and add a label at the top:

<li>

    <label>Blah</label>

    DISPLAY GROUP

</li>

Attempting to add a Label element to a display group results in the following error: Call to undefined method Zend_Form_DisplayGroup::getLabel() in Zend/Form/Decorator/Label.php on line 242.

Subclassing DisplayGroup and adding a getter and setter for Label results in: Call to undefined method SJCRH_Form_DisplayGroup::isRequired() in Zend/Form/Decorator/Label.php on line 260.

Obviously, I'm missing something in regards to DisplayGroup and how it relates to Decorators, but I can't seem to figure it out.  Any help?
Tim Fletcher

Re: Zend Form: Trying to add a Label Decorator to a DisplayGroup

Reply Threaded More More options
Print post
Permalink
It took me long time to work out that in order to subclass Zend_Form_DisplayGroup, you must set the class appropriately. If you're creating your form by subclassing Zend_Form you should use :

$this->setDefaultDisplayGroupClass('App_Form_DisplayGroup');
umpirsky

Re: Zend Form: Trying to add a Label Decorator to a DisplayGroup

Reply Threaded More More options
Print post
Permalink
Yes, or

$this->addDisplayGroup(
        array(
                'e1',
                'e2'
                ),
                'name',
                array(
                'displayGroupClass' => 'My_Form_DisplayGroup'
                )

But I get:

Warning: Exception caught by form: Method getType does not exist Stack Trace: #0 [internal function]: Zend_Form_DisplayGroup->__call('getType', Array) #1 /usr/local/share/pear/Zend/Form/Decorator/ViewHelper.php(92): Lib_Form_DisplayGroup->getType() #2 /usr/local/share/pear/Zend/Form/Decorator/ViewHelper.php(233): Zend_Form_Decorator_ViewHelper->getHelper() #3 /usr/local/share/pear/Zend/Form/DisplayGroup.php(874): Zend_Form_Decorator_ViewHelper->render('') #4 /usr/local/share/pear/Zend/Form/Decorator/FormElements.php(101): Zend_Form_DisplayGroup->render() #5 /usr/local/share/pear/Zend/Form.php(2626): Zend_Form_Decorator_FormElements->render('') #6 /usr/local/share/pear/Zend/Form.php(2641): Zend_Form->render() #7 /usr/local/www/svn.loopia.se/controlweb/umpirsky.test.af.loopia.se/application/views/scripts/userinfo/index.phtml(18): Zend_Form->__toString() #8 /usr/local/share/pear/Zend/View.php(108): include('/usr/local/www/...') #9 /usr/local/share/pear/Zend/View/Abstract.php(833): Zend_View->_run('/usr/local/www/...') #1 in /usr/local/share/pear/Zend/Form.php on line 2646

My class:

class My_Form_DisplayGroup extends Zend_Form_DisplayGroup {
    public function loadDefaultDecorators() {
        if ($this->loadDefaultDecoratorsIsDisabled()) {
            return;
        }

         $this->setDecorators(
                        array(
                            'ViewHelper',
                                array(array('td' => 'HtmlTag'), array('tag' => 'td', 'style' => 'width: 360px;')),
                                array('Label', array('tag' => 'td', 'requiredSuffix' => ' *')),
                                array(array('tr' => 'HtmlTag'), array('tag' => 'tr')),
                                array(array('tbody' => 'HtmlTag'), array('tag' => 'tbody')),
                            array(array('table' => 'HtmlTag'), array('tag' => 'table', 'class' => 'tiny')),
                            array(array('separator' => 'HtmlTag'), array('tag' => 'div', 'class' => 'separator'))
                        )
            );
    }

}

Any idea?


Tim Fletcher wrote:
It took me long time to work out that in order to subclass Zend_Form_DisplayGroup, you must set the class appropriately. If you're creating your form by subclassing Zend_Form you should use :

$this->setDefaultDisplayGroupClass('App_Form_DisplayGroup');
umpirsky

Re: Zend Form: Trying to add a Label Decorator to a DisplayGroup

Reply Threaded More More options
Print post
Permalink
Ah, wrong decorators - fixed!

Regards,
Saša Stamenković


On Fri, Nov 6, 2009 at 9:20 AM, umpirsky <[hidden email]> wrote:

Yes, or

$this->addDisplayGroup(
               array(
                       'e1',
                       'e2'
               ),
               'name',
               array(
                       'displayGroupClass' => 'My_Form_DisplayGroup'
               )

But I get:

Warning: Exception caught by form: Method getType does not exist Stack
Trace: #0 [internal function]: Zend_Form_DisplayGroup->__call('getType',
Array) #1 /usr/local/share/pear/Zend/Form/Decorator/ViewHelper.php(92):
Lib_Form_DisplayGroup->getType() #2
/usr/local/share/pear/Zend/Form/Decorator/ViewHelper.php(233):
Zend_Form_Decorator_ViewHelper->getHelper() #3
/usr/local/share/pear/Zend/Form/DisplayGroup.php(874):
Zend_Form_Decorator_ViewHelper->render('') #4
/usr/local/share/pear/Zend/Form/Decorator/FormElements.php(101):
Zend_Form_DisplayGroup->render() #5
/usr/local/share/pear/Zend/Form.php(2626):
Zend_Form_Decorator_FormElements->render('') #6
/usr/local/share/pear/Zend/Form.php(2641): Zend_Form->render() #7
/usr/local/www/svn.loopia.se/controlweb/umpirsky.test.af.loopia.se/application/views/scripts/userinfo/index.phtml(18):
Zend_Form->__toString() #8 /usr/local/share/pear/Zend/View.php(108):
include('/usr/local/www/...') #9
/usr/local/share/pear/Zend/View/Abstract.php(833):
Zend_View->_run('/usr/local/www/...') #1 in
/usr/local/share/pear/Zend/Form.php on line 2646

My class:

class My_Form_DisplayGroup extends Zend_Form_DisplayGroup {
   public function loadDefaultDecorators() {
       if ($this->loadDefaultDecoratorsIsDisabled()) {
           return;
       }

        $this->setDecorators(
                       array(
                           'ViewHelper',
                               array(array('td' => 'HtmlTag'), array('tag' => 'td', 'style' => 'width:
360px;')),
                               array('Label', array('tag' => 'td', 'requiredSuffix' => ' *')),
                               array(array('tr' => 'HtmlTag'), array('tag' => 'tr')),
                               array(array('tbody' => 'HtmlTag'), array('tag' => 'tbody')),
                           array(array('table' => 'HtmlTag'), array('tag' => 'table', 'class' =>
'tiny')),
                           array(array('separator' => 'HtmlTag'), array('tag' => 'div', 'class'
=> 'separator'))
                       )
           );
   }

}

Any idea?



Tim Fletcher wrote:
>
> It took me long time to work out that in order to subclass
> Zend_Form_DisplayGroup, you must set the class appropriately. If you're
> creating your form by subclassing Zend_Form you should use :
>
> $this->setDefaultDisplayGroupClass('App_Form_DisplayGroup');
>

--
View this message in context: http://old.nabble.com/Zend-Form%3A-Trying-to-add-a-Label-Decorator-to-a-DisplayGroup-tp18097521p26228255.html
Sent from the Zend Framework mailing list archive at Nabble.com.