Zend表格装饰问题

时间:2010-01-27 16:57:26

标签: zend-framework zend-form decorator

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

1 个答案:

答案 0 :(得分:2)

修复了包含“comments”元素的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')
    ));

有兴趣听听我是否有更好的方法可以做到这一点。