如何将数组(可定制)添加到 symfony2 表单(使用奏鸣曲管理员)? [英] How to add an array (customisable) to a symfony2 form (with sonata Admin)?

查看:28
本文介绍了如何将数组(可定制)添加到 symfony2 表单(使用奏鸣曲管理员)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个索纳塔管理员的简单表格.

I have a simple form with Sonata admin.

我希望用户可以添加一个整数列表(任意数量).然后它将作为数组存储在我的对象中:

I would like the user could add a list of integers (as many as he wants). And after it would be store as an array in my object:

[1, 2, 3, 6, 9]

[1, 2, 3, 6, 9]

有没有办法在不创建另一个类来实例化整数的情况下做到这一点?

There any way of doing it without creating another class to instantiate the integers?

更新:

我知道如何关闭的唯一方法是使用以下选项:

The only way I know how to something close is using choice like:

 ->add('type', 'choice', [
                "required"           => true,
                "expanded"           => true,
                "multiple"           => false,
                "choices"            => Campanha::getTypes(),
            ])

但是由于我的选择数量有限,我希望用户可以自由添加他想要的数字数量和值

But with that I have a limited number of choices, I would like that it would be free to the user to add the quantity of numbers and the values he wants

推荐答案

尝试查看 sonata_type_native_collection:

来自 奏鸣曲管理文档:

这个包通过添加处理原生的 Symfony 集合表单类型:

This bundle handle the native Symfony collection form type by adding:

  • 如果您将 allow_add 选项设置为 true,则为添加按钮.
  • 删除按钮,如果您将 allow_delete 选项设置为 true.

以及 Symfony 集合表单类型:

此字段类型用于呈现某个字段或表单的集合".在最简单的意义上,它可以是填充数组 emails 值的 TextType 字段数组.

This field type is used to render a "collection" of some field or form. In the easiest sense, it could be an array of TextType fields that populate an array emails values.

因此,对于您的情况,可能类似于:

So, for your case, maybe something like:

->add('type', 'sonata_type_native_collection', [
    'required' => true,
    'entry_type' => 'number',
    'options' => [
        // Any options you'd like the integer fields to have.
    ]
])

(当然,这与您需要对基础模型进行的更改完全没有关系.)

(This doesn't speak at all to the change's you'll need to make to the underlying model, of course.)

编辑:根据@Matheus Oliveira 的评论,将 'entry_options' 数组键更改为 'options'.

Edit: Changed the 'entry_options' array key to 'options', as per @Matheus Oliveira's comment.

这篇关于如何将数组(可定制)添加到 symfony2 表单(使用奏鸣曲管理员)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆