Zend framework zend表单装饰器
与zend form decorators有(更多)问题。到目前为止,我已经做到了: 重置整体窗体装饰器:Zend framework zend表单装饰器,zend-framework,zend-form,decorator,Zend Framework,Zend Form,Decorator,与zend form decorators有(更多)问题。到目前为止,我已经做到了: 重置整体窗体装饰器: $this->clearDecorators(); $this->setDecorators(array('FormElements', 'Form')); 我将所有元素添加到一个显示组中,我希望该显示组位于一个字段集中,一个DL中 $group->setDecorators(array( 'FormElements',
$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(array('ImageTag' => 'HtmlTag'), array('tag'=>'img', 'placement'=>'prepend', 'src'=>'/images/'.$imgs[$i-1]->im_name.'_main.jpg'))
));
从以下方面解释:
在内部,Zend_Form_元素在检索decorator时使用decorator的类作为查找机制。因此,您无法注册同一类型的多个装饰器;后续的装饰程序将简单地覆盖以前存在的装饰程序。
要解决这个问题,可以使用别名。不是将decorator或decorator名称作为第一个参数传递给addDecorator(),而是传递一个包含单个元素的数组,别名指向decorator对象或名称:
// Alias to 'FooBar':
$element->addDecorator(array('FooBar' => 'HtmlTag'),
array('tag' => 'div'));
// And retrieve later:
$decorator = $element->getDecorator('FooBar');
在addDecorators()和setDecorators()方法中,需要在表示decorator的数组中传递“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');
创建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')),
);
如您所见,我将第一个命名为“值”,第二个命名为“字段”。命名它们还使您能够在以后引用装饰器,如下所示:
$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);
非常感谢您提供这些信息!我现在也能用了 以下是完整的php代码供参考:
$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());
/感谢您的解释,这是有道理的,但不幸的是您的解决方案不起作用:(澄清一下,我最终得到的结果是:form>div>fieldset>dl div是字段集的包装器,它位于我希望图像位于的位置,尽管我得到了它,但您缺少了一个括号:array(array('ImageTag'=>'HtmlTag',应该是array(数组('ImageTag'=>'HtmlTag'),
$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());