Zend form decorators

decoratorzend-formzend-framework

Having (more) issues with zend form decorators. I've got this so far:

Reset overall form decorator:

    $this->clearDecorators();
    $this->setDecorators(array('FormElements', 'Form'));

I'm adding all my elements to a display group that i want to be inside a fieldset, within a DL

    $group->setDecorators(array(
           'FormElements',
            array('HtmlTag', array('tag' => 'dl')),
           'Fieldset'
    ));   

all working so far, now i want to place an image tag immediately before the fieldset. on its own this would work:

        $group->setDecorators(array(
            'FormElements',
            'Fieldset',
            array('HtmlTag',array('tag'=>'img','placement'=>'prepend','src'=>'/images/'.$imgs[$i-1]->im_name.'_main.jpg'))
        ));   

but this doesnt (it stops the DL being added inside the fieldset):

        $group->setDecorators(array(
            'FormElements',
            array('HtmlTag', array('tag' => 'dl')),
            'Fieldset',
            array('HtmlTag',array('tag'=>'img','placement'=>'prepend','src'=>'/images/'.$imgs[$i-1]->im_name.'_main.jpg'))
        ));

Where am i going wrong?

Best Answer

Wheh you create the HtmlTag decorators, give them names. Here's an example from my code:

protected $_fileElementDecorator = array(
    'File',
    array(array('Value'=>'HtmlTag'), array('tag'=>'span','class'=>'value')),
    'Errors',
    'Description',
    'Label',
    array(array('Field'=>'HtmlTag'), array('tag'=>'div','class'=>'field file')),
);

As you can see, I named the first one 'Value', and the second one 'Field'. Naming them also gives you the ability to reference the decorator later, like this:

$file = $form->getElement('upload_file');
$decorator = $file->getDecorator('Field');
$options = $decorator->getOptions();
$options['id'] = 'field_' . $file->getId();
if ($file->hasErrors()) {
    $options['class'] .= ' errors';
}
$decorator->setOptions($options);
Related Topic