-- Stefan Sturm <[EMAIL PROTECTED]> wrote
(on Friday, 08 August 2008, 02:12 PM +0200):
> I have an element decorator for my elements and it is working corrent.
> 
> It looks like this:
>       public $elementDecorators = array(
>               'ViewHelper',
>               'MyErrors',
>               array(array('data' => 'HtmlTag'), array('tag' => 'div', 'class' 
> =>
> 'element')),
>               array('Label', array('class' => 'left')),
>               array(array('row' => 'HtmlTag'), array('tag' => 'div', 'class' 
> => 'row')),
>       );
> 
> This renders code like this:
>       <div class="row">
>               <label for="txtUserName" class="required left 
> optional">Name</label>
>               <div class="element">
>                       <input type="text" name="txtUserName" id="txtUserName" 
> value=""
> size="30" maxlength="20" class="required" />
>               </div>
>       </div>
> 
> This is what I need :-)
> 
> But now my problem. Sometime I want to have two or more elements in
> one row. Like perhaps zipcode and city.
> This should be rendered like this:
>       <div class="row">
>               <label for="txtZipcode" class="required left 
> optional">Zipcode/City</label>
>               <div class="element">
>                       <input type="text" name="txtZipcode" id="txtZipcode" 
> value=""
> size="10" maxlength="10" class="required" />
>                       <input type="text" name="txtCity" id="txtCity" value="" 
> size="30"
> maxlength="20" class="required" />
>               </div>
>       </div>
> 
> 
> I think, that I need to use DisplayGroups to achieve this. But I don't
> get it to work :-(

What did you try? :)

Here's what I'd try: you'll need to create a custom decorator for the
display group, which I'll detail below, and then the following
element/group definitions.

    $form->addDisplayGroupPrefixPath('My_Form_Decorator', 'My/Form/Decorator');
    $form->addElement('text', 'txtZipcode', array(
        'size'       => 10,
        'maxlength'  => 10,
        'class'      => 'true',
        'required'   => 'true',
        'decorators' => array('ViewHelper'),
    ));
    $form->addElement('text', 'txtCity', array(
        'size'       => 30,
        'maxlength'  => 20,
        'class'      => 'true',
        'required'   => 'true',
        'decorators' => array('ViewHelper'),
    ));
    $form->addDisplayGroup(array('txtZipcode', 'txtCity'), 'zipCity', array(
        'legend'     => 'Zipcode/City',
        'decorators' => array(
            'FormElements',
            array('DivWrapper', array(
                'labelFor'   => 'txtZipcode', 
                'labelClass' => 'required left optional',
            ))
        ),
    ));

The DivWrapper decorator would look like this:

    My_Form_Decorator_DivWrapper extends Zend_Form_Decorator_Abstract()
    {
        public function render($content)
        {
            $group = $this->getElement();
            if (null === $group) {
                return $content;
            }

            $labelFor   = $this->getOption('labelFor');
            $labelClass = $this->getOption('labelClass');
            $label      = $group->getLegend();

            $html =<<<EOH
<div class="row">
    <label for="$labelFor" class="$labelClass">$label</label>
    <div class="element">
        $content
    </div>
</div>
EOH;
            return $html;
        }
    }

Put the above in My/Form/Decorator/DivWrapper.php on your include path,
and you should be set.

> I think this is a common problem. So hopefully some can give me a hint
> how my decorator needs to look like...

-- 
Matthew Weier O'Phinney
Software Architect       | [EMAIL PROTECTED]
Zend Framework           | http://framework.zend.com/

Reply via email to