2010-01-27 99 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') 
    )); 

會有興趣聽到有沒有更好的方法可以做到這一點。