duanbipu7601 2016-02-10 16:20
浏览 50

在Symfony FormType类中设置选项

I'm creating a form in Symfony2. The form has a field which is mapped to an entity array like this:

$builder
    ->add('card', 'entity', array(
        'class' => 'AppBundle:Card',
        'property' => 'description',
        'choices' => $choices
    ));

I load the $choices array in my Controller. If I create the Form using the FormBuilder in the Controller I can use easily set the choices option, but I'd like to keep it in its own class to avoid bloating the Controller. Is there a clean way to inject the $choices array when creating the form?

  • 写回答

1条回答 默认 最新

  • dowe98261 2016-02-10 17:08
    关注

    I'm not sure this is the best way, and it does seem a bit over-engineered, but I ended up defining the form as a service like this:

    my_custom_form_service:
        class: AppBundle\Form\MyFormType
        calls:
            - [ setUser, ["@security.context"] ]
        tags:
            - { name: form.type, alias: my_form }
    

    I needed the user object because I obtain the choices array from that entity.

    评论

报告相同问题?

悬赏问题

  • ¥20 西门子S7-Graph,S7-300,梯形图
  • ¥50 用易语言http 访问不了网页
  • ¥50 safari浏览器fetch提交数据后数据丢失问题
  • ¥15 matlab不知道怎么改,求解答!!
  • ¥15 永磁直线电机的电流环pi调不出来
  • ¥15 用stata实现聚类的代码
  • ¥15 请问paddlehub能支持移动端开发吗?在Android studio上该如何部署?
  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效
  • ¥15 悬赏!微信开发者工具报错,求帮改