当每个子表单的数量未知时如何处理 Zend SubForms
我有一个“客户”表单,其中有一个名为“联系人”的部分。首先,此联系人部分将包含以下元素。
<input type="text" name="contacts[0][fname]" />
<input type="text" name="contacts[0][sname]" />
但是用户可能想要添加另一个联系人,该联系人将使用 javascript 复制这些元素以生成以下内容:
<input type="text" name="contacts[0][fname]" />
<input type="text" name="contacts[0][sname]" />
<br />
<input type="text" name="contacts[1][fname]" />
<input type="text" name="contacts[1][sname]" />
我知道如何生成第一组元素,但是如果表单得到已提交但存在错误,如何确保渲染正确数量的“联系人”元素?
I have a 'customer' form which has a section called 'contacts'. To start with this contacts section will contain the following elements..
<input type="text" name="contacts[0][fname]" />
<input type="text" name="contacts[0][sname]" />
But the user may want to add another contact which will duplicate the elements with javascript to produce the following:
<input type="text" name="contacts[0][fname]" />
<input type="text" name="contacts[0][sname]" />
<br />
<input type="text" name="contacts[1][fname]" />
<input type="text" name="contacts[1][sname]" />
I know how to produce the first set of elements, however if the form gets submitted and there are errors, how can i ensure that the correct number of 'contacts' elements get rendered?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我从来没有用 Zend_Form 做过这个,但我已经用 Symfony 1.4 的 sfForm 做过了,它有类似的 API 和操作理论。基于此,基本过程是:
在父窗体构造函数中初始化一些默认数量的子窗体。您需要将实际创建 n 个子表单并将其嵌入到单独的方法中的逻辑分开。我将此称为方法
emebedContacts($count = 1)
重写父表单上的
isValid
和setDefaults
方法,以便它们检测传递给它们的$data
参数中的子表单数量,然后在调用parent::isValid()
之前调用embedContacts
或parent::setDefaults()
.希望有帮助。
Ive never had to do this with Zend_Form but i have done it with Symfony 1.4's sfForm which has a similar API and theory of operation. Based on that the basic process is:
In the parent forms constructor intialize some default number of subforms. Youll want to separate out the logic for actually creating and embedding
n
subforms into a separate method(s). Ill refer to this as the methodemebedContacts($count = 1)
Override the
isValid
andsetDefaults
methods on the parent form so that they detect the number of subforms in the$data
arguments passed to them and then callembedContacts
before callingparent::isValid()
orparent::setDefaults()
.Hope that helps.