开发者

zend form decorators

开发者 https://www.devze.com 2022-12-18 22:12 出处:网络
Having (more) issues with zend form decorators. I\'ve got this so far: Reset overall form decorator: $this->clearDecorators();

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('t开发者_如何学Pythonag'=>'img','placement'=>'prepend','src'=>'/images/'.$imgs[$i-1]->im_name.'_main.jpg'))
        ));

Where am i going wrong?


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);


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

Explination from the manual : Internally, Zend_Form_Element uses a decorator's class as the lookup mechanism when retrieving decorators. As a result, you cannot register multiple decorators of the same type; subsequent decorators will simply overwrite those that existed before. To get around this, you can use aliases. Instead of passing a decorator or decorator name as the first argument to addDecorator(), pass an array with a single element, with the alias pointing to the decorator object or name:

// Alias to 'FooBar':
$element->addDecorator(array('FooBar' => 'HtmlTag'),
                       array('tag' => 'div'));

// And retrieve later:
$decorator = $element->getDecorator('FooBar');

In the addDecorators() and setDecorators() methods, you will need to pass the 'decorator' option in the array representing the decorator:

// Add two 'HtmlTag' decorators, aliasing one to 'FooBar':
$element->addDecorators(
    array('HtmlTag', array('tag' => 'div')),
    array(
        'decorator' => array('FooBar' => 'HtmlTag'),
        'options' => array('tag' => 'dd')
    ),
);

// And retrieve later:
$htmlTag = $element->getDecorator('HtmlTag');
$fooBar  = $element->getDecorator('FooBar');


Thank you so much for this information! I got it working now as well.

This is the complete php code FYI:

    $generatePhraseVariations = new Zend_Form_Element_Checkbox('generatephrasevariations');
    $generatePhraseVariations->setLabel('Generate phrase variations')
        ->setCheckedValue('yes')
        ->setUncheckedValue('no')
        ->setChecked(TRUE)
        ->setDecorators($this->myCheckBoxElementDecorators);
    $generateSpellingMistakes = new Zend_Form_Element_Checkbox('generatespellingmistakes');
    $generateSpellingMistakes->setLabel('Generate Spelling Mistakes')
        ->setCheckedValue('yes')
        ->setUncheckedValue('no')
        ->setChecked(FALSE)
        ->setDecorators($this->myCheckBoxElementDecorators);
    $this->addElements(array($generatePhraseVariations,$generateSpellingMistakes));
    $this->addDisplayGroup( 
        array('generatephrasevariations','generatespellingmistakes'),
        'rightpanel1');
    Zend_Registry::get('logger')->info($this->getDisplayGroup('rightpanel1')->getDecorators());
    $this->getDisplayGroup('rightpanel1')
        ->setLegend('Features') 
        ->setDecorators(
            array(
                'FormElements',
                array(array('Mijn-OL-HtmlTag'=>'HtmlTag'),array('tag'=>'ol')),
                array('Fieldset'),
                array(array('Mijn-DIV-HtmlTag'=>'HtmlTag'),array('tag'=>'div','id'=>'rightpanel1')),
                )
        );
    Zend_Registry::get('logger')->info($this->getDisplayGroup('rightpanel1')->getDecorators());

/

0

精彩评论

暂无评论...
验证码 换一张
取 消