使用 jQuery 和 Zend_Form 动态添加表单元素
我有一个表单,人们可以在其中使用加号按钮添加元素的相同部分,从而生成类似的内容:
<div id="person-1" class="person">
<input type="text" name="name-1" id="name-1" />
<input type="text" name="age-1" id="age-1" />
</div>
<!-- as of here, it's JS created -->
<div id="person-2" class="person">
<input type="text" name="name-2" id="name-2" />
<input type="text" name="age-2" id="age-2" />
</div>
<div id="person-3" class="person">
<input type="text" name="name-3" id="name-3" />
<input type="text" name="age-3" id="age-3" />
</div>
我已经设法编写了 jquery 代码,该代码允许我再次添加相同的元素一个新的 ID(姓名-1、年龄-1、姓名-2、年龄-2、姓名-3、年龄-3,...)。
当然,Zend_Form 不知道 name-2 和 name-3,因此当表单包含错误并再次显示时,它只是删除它们。我也无法使用 $form->getValue('name-2')
访问 name-2 的值。我必须检查原始 $this->getRequest()->getPost()
。
有没有更好的方法可以用来组合 Zend_Form 和基于 javascript 的添加表单元素(与硬编码元素的类型相同)。
警告:在真正的问题中,是选择
而不是输入
。发现这可能会产生影响(使用 ->setIsArray(true)
),但使用 select
会破坏示例代码。
I have a form in which people shall be able to add the same portion of elements with a plus-button, so that something like this is produced:
<div id="person-1" class="person">
<input type="text" name="name-1" id="name-1" />
<input type="text" name="age-1" id="age-1" />
</div>
<!-- as of here, it's JS created -->
<div id="person-2" class="person">
<input type="text" name="name-2" id="name-2" />
<input type="text" name="age-2" id="age-2" />
</div>
<div id="person-3" class="person">
<input type="text" name="name-3" id="name-3" />
<input type="text" name="age-3" id="age-3" />
</div>
I already managed to write jquery-code that allows me to add the same elements once again with a new id (name-1, age-1, name-2, age-2, name-3, age-3, …).
Of course, Zend_Form does not know about name-2 and name-3, so it just drops them when the form contains an error and is displayed again. Neither can I access the value of name-2 with $form->getValue('name-2')
. I have to go over raw $this->getRequest()->getPost()
.
Is there a better method I can use to combine Zend_Form and javascript-based added form elements (of same type like an hardcoded element).
Caveat: In the real problem, it’s select
and not input
. Found out this could make a difference (with ->setIsArray(true)
), but using select
would blow up the example code.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以做的是在主窗体内创建一个子窗体容器,并向该容器添加 X 数量的子窗体。
例如:
渲染时,输入字段的名称将类似于
persons[1][name]
。注意$index+1
,Zend_Form 不允许表单被命名为“0”。当然,只有在人员子表单的数量有限时才应该使用此方法。
另一种策略是重写 isValid 方法并使用单个 My_PersonForm 表单来验证所有人员数据。
旁注;仅当您在创建表单实例时将 numPersons 定义为选项集的一部分时,上述代码才有效。例如;
$form = new My_Form(array('numPersons' => 10));
What you could do is create a subform container inside your main form and add an X amount of subforms to that container.
For example:
When rendered, the input fields will have names like
persons[1][name]
. Note the$index+1
, Zend_Form does not allow a form to be named '0'.Ofcourse, you should only use this method if the amount of person subforms is limited.
Another strategy would be to override the
isValid
method and use a singleMy_PersonForm
form to validate all the person data.Sidenote; the above code will only work when you define the numPersons as part of the options set, when creating the form instance. E.g.;
$form = new My_Form(array('numPersons' => 10));