2010-01-27 106 views
2

我有一些与装饰相关的东西与Zend窗体的随机问题。Zend表格装饰问题

首先,

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

心不是加入一个字段但使用相同代码的自定义表单元素添加到其自己的字段集中:

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

回答

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

会有兴趣听到有没有更好的方法可以做到这一点。