2014-10-20 48 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 
     } 
    }