2010-01-28 79 views
2

具有(更多)zend表單裝飾器的問題。我得到這個至今: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')) 
     )); 

我在哪裏去了?從manual

回答

5

Wheh創建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')), 
); 

正如你所看到的,我命名的第一個「值」,而第二個「田」。命名他們也給你以後引用裝飾,這樣的能力:

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

Explination: 內部,Zend_Form_Element的檢索裝飾時使用裝飾器的類作爲查詢機制。因此,你不能註冊多個相同類型的裝飾器;後續的裝飾器會簡單地覆蓋之前存在的裝飾器。 爲了解決這個問題,你可以使用別名。代替傳遞裝飾或裝飾器名稱爲第一參數給addDecorator()的,通過陣列與單個元件,與別名指向裝飾對象或名稱:

// Alias to 'FooBar': 
$element->addDecorator(array('FooBar' => 'HtmlTag'), 
         array('tag' => 'div')); 

// And retrieve later: 
$decorator = $element->getDecorator('FooBar'); 

在addDecorators()和setDecorators ()方法中,你需要通過「裝飾」選項表示裝飾器陣列中:

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

感謝您的解釋,這是有道理的,但不幸的是你的解決方案不起作用:( – robjmills 2010-01-28 15:09:46

+0

澄清我結束了: form> div> fieldset> dl Div是一個圍繞字段集的包裝,它的位置是我想要的圖像在 – robjmills 2010-01-28 15:11:24

+0

我知道了,你錯過了一個括號:array(array('ImageTag'=>' HtmlTag',應該是數組(array('ImageTag'=>'HtmlTag'), – robjmills 2010-01-28 15:19:01

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

/