是否可以創建一個空的顯示組,然後添加元素?zend框架創建空的顯示組,然後添加元素
我想:
addDisplayGroup(null, 'exampleGroup')
但是你必須通過至少一個元素......
我需要創建只是一個空顯示組。
我必須這樣做,因爲創建顯示組之後,我會通過一個在foreach元素添加一個....
再次感謝......
是否可以創建一個空的顯示組,然後添加元素?zend框架創建空的顯示組,然後添加元素
我想:
addDisplayGroup(null, 'exampleGroup')
但是你必須通過至少一個元素......
我需要創建只是一個空顯示組。
我必須這樣做,因爲創建顯示組之後,我會通過一個在foreach元素添加一個....
再次感謝......
您可以用
嘗試$form->addDisplayGroup(array(), 'exampleGroup');
,然後得到顯示組這樣的:
$displayGroup = $form->getDisplayGroup('exampleGroup');
,並添加的Elemen隨後用$displayGroup->addElement()
或$displayGroup->createElement()
一個接一個。
您可以在Display Groups documentation中查看更多詳情。
EDIT:
ZF不允許添加一個顯示組沒有元素。最簡單的解決方案可能是首先創建元素,將它們放入數組中,然後將該數組傳遞給addDisplayGroup
。
它也可以實例化一個新Zend_Form_DisplayGroup
對象,然後將其添加到使用$form->addDisplayGroups(array($group))
形式,但我懷疑這是不是調用addDisplayGroup()
在已創建的元素更加方便。
希望幫助,
如何覆蓋表單的addDisplayGroup($elements, $name)
方法,添加如果虛設的元素必要的:
class My_Form extends Zend_Form
{
protected $dummyName = '_myDummy';
public function addDisplayGroup(array $elements, $name)
{
if (count($elements) == 0){
$elt = new Zend_Form_Element_Hidden($this->dummyName);
$elt->setIgnore(true);
$elements[] = $this->dummyName;
}
return parent::addDisplayGroup($elements, $name);
}
public function render(Zend_View_Interface $view = null)
{
$this->removeElement($this->dummyName);
return parent::render($view);
}
}
然後在你的表格,您現在可以爲調用:
$displayGroup = $this->addDisplayGroup(array(), 'myDisplayGroup');
$elt1 = new Zend_Form_Element_Text('myElement1');
$displayGroup->addElement($elt1);
$elt2 = new Zend_Form_Element_Text('myElement2');
$displayGroup->addElement($elt2);
// etc
只需將你的foreach中的元素放到一個數組中,然後將它傳遞給 - > addDisplayGroup – max4ever
嗨,Max,是的,最後我做了這樣的事情......我爲此而努力......我只是想以另一種方式,因爲代碼看起來更容易理解...無論如何是好的...再次感謝.... – Samuele