dongyi1921 2016-10-14 15:28
浏览 45
已采纳

如何将数组(可自定义)添加到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]

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

UPDATE:

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

  • 写回答

2条回答 默认 最新

  • douan0729 2016-10-14 21:26
    关注

    Try looking into sonata_type_native_collection:

    From the Sonata Admin Docs:

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

    • an add button if you set the allow_add option to true.
    • a delete button if you set the allow_delete option to true.

    And the Symfony collection form type:

    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.)

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

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 目前主流的音乐软件,像网易云音乐,QQ音乐他们的前端和后台部分是用的什么技术实现的?求解!
  • ¥60 pb数据库修改与连接
  • ¥15 spss统计中二分类变量和有序变量的相关性分析可以用kendall相关分析吗?
  • ¥15 拟通过pc下指令到安卓系统,如果追求响应速度,尽可能无延迟,是不是用安卓模拟器会优于实体的安卓手机?如果是,可以快多少毫秒?
  • ¥20 神经网络Sequential name=sequential, built=False
  • ¥16 Qphython 用xlrd读取excel报错
  • ¥15 单片机学习顺序问题!!
  • ¥15 ikuai客户端多拨vpn,重启总是有个别重拨不上
  • ¥20 关于#anlogic#sdram#的问题,如何解决?(关键词-performance)
  • ¥15 相敏解调 matlab