dsfdsf8888 2015-06-19 23:55
浏览 16
已采纳

如何在创建表单时传递选项

I'm working on a form and want to pass to my form an array that would content all my projects so that my form would have a select with all of those project for option.

I already reed alot of answer, but I just can't figure this thing out. I know that I'm suppose to do something like:

$formulaire=$this->createForm(new ModifierSupprimerProjet(), null, $myArray);

but, I'm suppose to add all the content of my array to getDefaultOptions()... I do I do that? An other thing, what is suppose to be the second parameters of the createForm method?

Here is the post that that came the closest to solve my probleme:

Accessing a variable through $options in the buildForm()

  • 写回答

3条回答 默认 最新

  • doucong7963 2015-06-20 05:08
    关注

    The best way to populate select form element with Project entities is to use entity form field. So you don't have to pass any options to form type class.

    $builder
        ->add('project', 'entity', [
            'label'         => 'form.project',
            'class'         => 'AppBundle\Entity\Project',
            'property'      => 'name',
            'required'      => true,
            'empty_value'   => 'form.select_empty_value',
            'query_builder' => function($repository) {
                return $repository->createQueryBuilder('p')->add('orderBy', 'p.name ASC');
            },
        ])
    ;
    

    If you need to populate choice field there is another best practice. You should define your form type class as service and inject Entity Manager into it. So you could fetch any data from the database and populate any choice fields.

    services.xml

    <service id="app.form.modifier_supprimer_projet" class="AdminBundle\Form\ModifierSupprimerProjet">
        <tag name="form.type" alias="app_modifier_supprimer_projet" />
        <argument type="service" id="doctrine.orm.entity_manager"/>
    </service>
    

    ModifierSupprimerProjet.php

    <?php
    
    namespace AppBundle\Form;
    
    use Symfony\Component\Form\AbstractType;
    use Symfony\Component\Form\FormBuilderInterface;
    use Symfony\Component\OptionsResolver\OptionsResolverInterface;
    use Doctrine\ORM\EntityManager;
    
    class ModifierSupprimerProjet extends AbstractType
    {
        /**
         * @var EntityManager
         */
        protected $em;
    
        /**
         * @param EntityManager $em
         */
        public function __construct(EntityManager $em)
        {
            $this->em = $em;
        }
    
        /**
         * @param FormBuilderInterface $builder
         * @param array $options
         */
        public function buildForm(FormBuilderInterface $builder, array $options)
        {
            $builder
                ->add('project', 'choice', [
                    'label'   => 'form.project',
                    'choices' => $this->em->getRepository('AppBundle:Project')->getProjectsList(),
                ])
            ;
        }
    
        /**
         * @param OptionsResolverInterface $resolver
         */
        public function setDefaultOptions(OptionsResolverInterface $resolver)
        {
            $resolver->setDefaults([    
                'data_class' => 'AppBundle\Entity\ModifierSupprimerProjet',
            ]);     
        }
    
        /**
         * @return string
         */
        public function getName()
        {
            return 'app_modifier_supprimer_projet';
        }
    }
    

    Controller

    $modifierSupprimerProjet = new ModifierSupprimerProjet();
    
    $form = $this->createForm('app_modifier_supprimer_projet', $modifierSupprimerProjet);
    

    Second param of createForm method is Entity, if your form is mapped to any entity.

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

报告相同问题?

悬赏问题

  • ¥15 使用ue5插件narrative时如何切换关卡也保存叙事任务记录
  • ¥20 软件测试决策法疑问求解答
  • ¥15 win11 23H2删除推荐的项目,支持注册表等
  • ¥15 matlab 用yalmip搭建模型,cplex求解,线性化处理的方法
  • ¥15 qt6.6.3 基于百度云的语音识别 不会改
  • ¥15 关于#目标检测#的问题:大概就是类似后台自动检测某下架商品的库存,在他监测到该商品上架并且可以购买的瞬间点击立即购买下单
  • ¥15 神经网络怎么把隐含层变量融合到损失函数中?
  • ¥15 lingo18勾选global solver求解使用的算法
  • ¥15 全部备份安卓app数据包括密码,可以复制到另一手机上运行
  • ¥20 测距传感器数据手册i2c