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 微信会员卡接入微信支付商户号收款
  • ¥15 如何获取烟草零售终端数据
  • ¥15 数学建模招标中位数问题
  • ¥15 phython路径名过长报错 不知道什么问题
  • ¥15 深度学习中模型转换该怎么实现
  • ¥15 HLs设计手写数字识别程序编译通不过
  • ¥15 Stata外部命令安装问题求帮助!
  • ¥15 从键盘随机输入A-H中的一串字符串,用七段数码管方法进行绘制。提交代码及运行截图。
  • ¥15 TYPCE母转母,插入认方向
  • ¥15 如何用python向钉钉机器人发送可以放大的图片?