添加相同类型的多个子表单

Nic*_*nay 4 php zend-framework zend-form multidimensional-array subforms

我最近正在处理表格和子表格.

我创建了以下内容:

$form = new Application_Form_Cv();
$experience = new Zend_Form_SubForm();
$form->addSubForm($experience, 'experience');
Run Code Online (Sandbox Code Playgroud)

我也有我的数组中的元素'experience'感谢

$form->addSubForm($experience, 'experience');.
Run Code Online (Sandbox Code Playgroud)

当我尝试以下内容时:

$experience->addSubForm(new Application_Form_Experience(), '0');
$experience->addSubForm(new Application_Form_Experience(), '1');
Run Code Online (Sandbox Code Playgroud)

该对象覆盖自身,我只获得一个'体验'元素,0和1不存在.

array (
  'controller' => 'cv',
  'action' => 'index',
  'module' => 'default',
  'CvName' => 'Cv Ingenieur informatique',
  'LanguageCode' => 'fr',
  'UserID' => '2',
  'experience' => 
  array (
    'CompanyName' => 'Mondial Assistance Ltd',
    'From' => '2002',
    'Until' => '2009',
    'Current' => '1',
  ),
  'submit' => 'Save CV',
)  
Run Code Online (Sandbox Code Playgroud)

只Zend_Form_Subforms在数组中创建新键?

Ben*_*mer 6

  1. 你的子表单必须扩展Zend_Form_SubForm或模仿它的行为(设置isArray并删除"form"-decorator)
  2. 您不能两次添加idenentical对象,因此您必须克隆它

以下剪辑应该按预期工作

$form = new Application_Form_Cv();
$experience = new Zend_Form_SubForm();
$form->addSubForm($experience, 'experience');

$exForm = new Application_Form_Experience();
$exForm->setIsArray(true);
$exForm->removeDecorator('form');

$experience->addSubForm($exForm, '0');
$experience->addSubForm(clone $exForm, '1');
$experience->addSubForm(clone $exForm, '2');
Run Code Online (Sandbox Code Playgroud)