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 eda:门禁系统设计
  • ¥50 如何使用js去调用vscode-js-debugger的方法去调试网页
  • ¥15 376.1电表主站通信协议下发指令全被否认问题
  • ¥15 物体双站RCS和其组成阵列后的双站RCS关系验证
  • ¥15 复杂网络,变滞后传递熵,FDA
  • ¥20 csv格式数据集预处理及模型选择
  • ¥15 部分网页页面无法显示!
  • ¥15 怎样解决power bi 中设置管理聚合,详细信息表和详细信息列显示灰色,而不能选择相应的内容呢?
  • ¥15 QTOF MSE数据分析
  • ¥15 平板录音机录音问题解决