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 想问一下树莓派接上显示屏后出现如图所示画面,是什么问题导致的
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line
  • ¥500 火焰左右视图、视差(基于双目相机)
  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号