【问题标题】:zend form decoratorsZend 表单装饰器
【发布时间】:2011-01-10 10:48:19
【问题描述】:

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

我哪里错了?

【问题讨论】:

    标签: zend-framework zend-form decorator


    【解决方案1】:

    非常感谢您提供这些信息!我现在也能正常工作了。

    这是完整的 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());
    

    /

    【讨论】:

      【解决方案2】:
      $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');
      

      【讨论】:

      • 感谢您的解释,这是有道理的,但不幸的是您的解决方案不起作用:(
      • 澄清我最终得到:form > div > fieldset > dl Div 是 fieldset 的包装器,它位于我希望图像所在的位置
      • 我明白了,你少了一个括号:array(array('ImageTag' => 'HtmlTag', 应该是 array(array('ImageTag' => 'HtmlTag'),跨度>
      【解决方案3】:

      当你创建 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);
      

      【讨论】:

        猜你喜欢
        • 2012-10-23
        • 2011-07-13
        • 2011-01-24
        • 1970-01-01
        • 2011-06-24
        • 2012-04-18
        • 2011-08-08
        • 2011-10-07
        • 1970-01-01
        相关资源
        最近更新 更多