zend表单装饰器

rob*_*lls 2 zend-framework decorator zend-form

与zend表单装饰器有(更多)问题.到目前为止我有这个:

重置整体表单装饰器:

    $this->clearDecorators();
    $this->setDecorators(array('FormElements', 'Form'));
Run Code Online (Sandbox Code Playgroud)

我正在将我的所有元素添加到我想要在DL中的字段集内的显示组中

    $group->setDecorators(array(
           'FormElements',
            array('HtmlTag', array('tag' => 'dl')),
           'Fieldset'
    ));   
Run Code Online (Sandbox Code Playgroud)

所有工作到目前为止,现在我想在字段集前面放置一个图像标记.这本身就可以了:

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

但这没有(它会停止在字段集中添加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'))
        ));
Run Code Online (Sandbox Code Playgroud)

我哪里错了?

Son*_*nny 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')),
);
Run Code Online (Sandbox Code Playgroud)

如您所见,我将第一个命名为"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);
Run Code Online (Sandbox Code Playgroud)