当每个子表单的数量未知时如何处理 Zend SubForms

发布于 2025-01-06 05:35:42 字数 557 浏览 2 评论 0原文

我有一个“客户”表单,其中有一个名为“联系人”的部分。首先,此联系人部分将包含以下元素。

<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 技术交流群。

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

发布评论

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

评论(1

旧人九事 2025-01-13 05:35:42

我从来没有用 Zend_Form 做过这个,但我已经用 Symfony 1.4 的 sfForm 做过了,它有类似的 API 和操作理论。基于此,基本过程是:

  1. 在父窗体构造函数中初始化一些默认数量的子窗体。您需要将实际创建 n 个子表单并将其嵌入到单独的方法中的逻辑分开。我将此称为方法 emebedContacts($count = 1)

  2. 重写父表单上的 isValidsetDefaults 方法,以便它们检测传递给它们的 $data 参数中的子表单数量,然后在调用 parent::isValid() 之前调用 embedContactsparent::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:

  1. 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 method emebedContacts($count = 1)

  2. Override the isValid and setDefaults methods on the parent form so that they detect the number of subforms in the $data arguments passed to them and then call embedContacts before calling parent::isValid() or parent::setDefaults().

Hope that helps.

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