gpt4 book ai didi

Symfony 表单 : Collect array of data

转载 作者:行者123 更新时间:2023-12-03 08:00:11 27 4
gpt4 key购买 nike

我需要通过输入字段收集数据数组。例如:

<input name="customer_name" type="text">
<input name="customer_emails[]" type="text">
<input name="customer_emails[]" type="text">
<input name="customer_emails[]" type="text">

如何在 Symfony2 表单生成器中呈现这种类型的表单?

文档说:

$builder->add('emails', 'collection', array(
// each item in the array will be an "email" field
'type' => 'email',
// these options are passed to each "email" type
'options' => array(
'required' => false,
'attr' => array('class' => 'email-box')
),
));

我不清楚应该在哪里提供我想要的电子邮件字段数量。

最佳答案

集合字段的确切数量由绑定(bind)到表单的模型确定。仔细看看 Symfony 的 collection field type documentation 的最后一段:

In both cases, no input fields would render unless your emails data array already contained some emails.

In this simple example, it's still impossible to add new addresses or remove existing addresses. Adding new addresses is possible by using the allow_add option (and optionally the prototype option) (see example below). Removing emails from the emails array is possible with the allow_delete option.

因此,如果您需要接受三个电子邮件地址,请确保绑定(bind)到表单的数据设置了相应的值,或者考虑集合的'allow_add'选项 类型。

关于Symfony 表单 : Collect array of data,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25237827/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com