【问题标题】:Zend Form Decoration IssuesZend 表单装饰问题
【发布时间】:2011-01-10 01:31:34
【问题描述】:

我在 Zend Form 中遇到了一些与装饰器相关的随机问题。

首先,

    // THIS WORKS AND REMOVES THE DECORATORS
    $hidden = new Zend_Form_Element_Hidden('hiddenfield');
    $hidden->setRequired(TRUE)
           ->removeDecorator('label')
           ->removeDecorator('HtmlTag')
           ->addErrorMessage('Please upload something');

    // BUT IT DOESNT WORK HERE - THE DECORATORS ARENT REMOVED
    $submit = new Zend_Form_Element_Submit('submit');
    $submit->setLabel('Proceed to Part 2 of 2')
           ->removeDecorator('label')
           ->removeDecorator('HtmlTag')
           ->setAttrib('class', 'button fleft cta');

其次,这样创建一个表单元素:

    $comments = new Zend_Form_Element_Textarea('comments');
    $comments->setLabel('Any comments')
             ->setRequired(FALSE);

并像这样添加到显示组中:

    // THIS DOESNT WORK
    $this->addDisplayGroup(array('comments'),'comments');
    $comms = $this->getDisplayGroup('comments');
    $comms->setDecorators(array(
            'FormElements',
            array('HtmlTag', array('tag' => 'dl')),
            'Fieldset'
    ));

未添加到 Fieldset,但使用相同代码的自定义表单元素已添加到其自己的字段集:

    // THIS WORKS!
    $this->addDisplayGroup(array('custom'),'custom',array('legend'=>'Legend Here'));
    $swfupload = $this->getDisplayGroup('swfupload');
    $swfupload->setDecorators(array(
            'FormElements',
            array('HtmlTag', array('tag' => 'dl')),
            'Fieldset'
    ));

【问题讨论】:

    标签: zend-framework zend-form decorator


    【解决方案1】:

    修复了包含“cmets”元素的 displayGroup 的问题。显然,显示组不可能与其中包含的表单元素之一具有相同的名称。所以解决方案是这样的:

    // THIS DOESNT WORK
    $this->addDisplayGroup(array('comments'),'comments');
    $comms = $this->getDisplayGroup('comments');
    $comms->setDecorators(array(
            'FormElements',
            array('HtmlTag', array('tag' => 'dl')),
            'Fieldset'
    ));
    
    // THIS NOW WORKS
    $this->addDisplayGroup(array('comments'),'commentsbox'); // change here
    $comms = $this->getDisplayGroup('commentsbox'); // change here
    $comms->setDecorators(array(
            'FormElements',
            array('HtmlTag', array('tag' => 'dl')),
            'Fieldset'
    ));
    

    并通过从之前的大量 addElements 中删除提交并单独将其添加到表单中来修复另一个问题,手动删除装饰器,如下所示:

        $this->addElement($submit);
        $submit->setDecorators(array(
            array('ViewHelper'),
            array('Description'),
            array('HtmlTag')
        ));
    

    我很想知道是否有更好的方法可以做到这一点。

    【讨论】:

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