与zend表单装饰器有(更多)问题。到目前为止我已经有了这个:
重置整体表单装饰器:
$this->clearDecorators();
$this->setDecorators(array('FormElements', 'Form'));
我将所有元素添加到我希望位于字段集内的显示组中,在DL
中 $group->setDecorators(array(
'FormElements',
array('HtmlTag', array('tag' => 'dl')),
'Fieldset'
));
到目前为止所有工作,现在我想在字段集前面放置一个图像标记。它本身就可以工作:
$group->setDecorators(array(
'FormElements',
'Fieldset',
array('HtmlTag',array('tag'=>'img','placement'=>'prepend','src'=>'/images/'.$imgs[$i-1]->im_name.'_main.jpg'))
));
但这没有(它会停止在字段集中添加DL):
$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'))
));
我哪里错了?
答案 0 :(得分:5)
您创建HtmlTag装饰器时,请为它们命名。这是我的代码中的一个例子:
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')),
);
如您所见,我将第一个命名为'Value',将第二个命名为'Field'。命名它们还使您能够在以后引用装饰器,如下所示:
$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);
答案 1 :(得分:1)
$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'))
));
来自manual的爆炸: 在内部,Zend_Form_Element在检索装饰器时使用装饰器的类作为查找机制。因此,您无法注册多个相同类型的装饰器;后续装饰器将简单地覆盖之前存在的装饰器。 要解决这个问题,您可以使用别名。不是将装饰器或装饰器名称作为addDecorator()的第一个参数传递,而是传递一个带有单个元素的数组,别名指向装饰器对象或名称:
// Alias to 'FooBar':
$element->addDecorator(array('FooBar' => 'HtmlTag'),
array('tag' => 'div'));
// And retrieve later:
$decorator = $element->getDecorator('FooBar');
在addDecorators()和setDecorators()方法中,您需要在表示装饰器的数组中传递'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');
答案 2 :(得分:1)
非常感谢您提供此信息!我现在也开始工作了。
这是完整的PHP代码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());
/