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

发布于 2024-11-02 15:12:20 字数 1008 浏览 0 评论 0原文

我最近正在使用表单和子表单。

我创建了以下内容:

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

并且我的数组中确实有一个元素 'experience' 感谢

$form->addSubForm($experience, 'experience');.

当我尝试以下操作时:

$experience->addSubForm(new Application_Form_Experience(), '0');
$experience->addSubForm(new Application_Form_Experience(), '1');

该对象会覆盖自身,我只得到一个 'experience' 元素和 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',
)  

只有 Zend_Form_Subforms 在数组中创建新键?

I'm working with forms and sub forms lately.

I've created the following:

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

and I do have in my array an element 'experience' thanks to

$form->addSubForm($experience, 'experience');.

When I try the following:

$experience->addSubForm(new Application_Form_Experience(), '0');
$experience->addSubForm(new Application_Form_Experience(), '1');

The object overwrites itself and I get only one 'experience' element and 0 and 1 are not present.

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',
)  

Only Zend_Form_Subforms creates new keys in an array?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

瞄了个咪的 2024-11-09 15:12:20
  1. 你的子表单必须扩展 Zend_Form_SubForm 或模仿它的行为(设置 isArray 并删除“form”装饰器)
  2. 你不能添加相同的对象两次,所以你必须克隆它

以下片段应该按预期工作

$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');
  1. Your Subforms have to extend Zend_Form_SubForm or mimic it's behaviour (set isArray and remove "form"-decorator)
  2. You can't add the idenentical object twice so you have to clone it

The following snipped should work as expected

$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');
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文