2014-10-20 100 views
1

我是新來的Zend框架,我想創建一個窗體,除了從簡單的輸入和選擇字段還將包含多個輸入字段與jquery例如: Multiple input fieldsZend表單創建動態添加元素

和可能接受多個對選擇並輸入這樣的另一種形式的元素: Multiple pairs of select and input

那些將在客戶端側使用jquery插入。我如何在Zend中使用表單元素來實現它?

回答

0

我已經完成了你想要在某些項目中實現的目標,而且我發現的最佳方法是使用子窗體。以下是一些可能對您有幫助的指導方針。

配置父窗體

public function __construct($wine = null) 
{ 
    parent::__construct(); 

    $this->setName('parent-form'); // you can set any name 
    $this->setIsArray(true); 
    $this->_initForm(); 
} 

添加儘可能多的窗體,你需要你的表單

protected function _initForm(){ 
    $this->clearSubForms(); 
    $subForm = new Application_Form_Subform(1); 
    $subForm->removeDecorator('Form'); 
    $this->addSubform($subForm,'subform-'.1); 

    $subForm = new Application_Form_Subform(2); 
    $subForm->removeDecorator('Form'); 
    $this->addSubform($subForm,'subform-'.2); 
} 

配置子窗體裏面

public function __construct($key) { 
    parent::__construct(); 
    $this->setElementsBelongTo("parentform[subform][$key]"); 
    $this->setIsArray(true)->setName("subform")->setAttrib('enctype', 'multipart/form-data'); 

    $this->_initForm(); 
} 

迴響在你的表單視圖

<?php 
    foreach($this->element->getSubforms() as $key => $subForm){ 
     echo $subForm; 
    } 
?> 

子窗體上動態使用jQuery

我不會在這裏提供任何代碼表單視圖中添加元素。但是我通常做的是爲子表單添加一個隱藏的模板,以便從中進行克隆。您還需要做的是調整子窗體輸入上的名稱attibute的索引。

獲取有關控制器的數據

if ($request->isPost()) { 

    $data = $request->getPost('parentform'); 

    if ($form->isValid($data)) { 
     foreach ($data['subform'] as $subformInfo) { // loop through each subform input 
      // do something with your data 
     } 
    }