【问题标题】:How to set a data by default in a drop-down list如何在下拉列表中默认设置数据
【发布时间】:2019-10-08 12:52:19
【问题描述】:

在 symfony3.4 项目中,我有两个实体:Personne 和 Nationalite

在我的表单下拉列表中,我想默认选择 Nationalite 'French'。

PersonneType.php:

...
public function buildForm(FormBuilderInterface $builder, array $options) {
    $builder->add('nationalite', EntityType::class, array(
                        'class' => 'AppBundle:Nationalite',
                        'choice_label' => 'libelle',
                        'required' => false,
                        'empty_data' => function(NationaliteRepository $repo) {
                            return $repo->getNationaliteParDefaut();
                        }
                    ))
->add...

在 NationaliteRepository.php 中:

...
    public function getNationaliteParDefaut(){
        $qb = $this->createQueryBuilder('n');
        $qb->where($qb->expr()->eq('n.codeInsee', ':code_insee'))
            ->setParameter('code_insee', 99100); //99100 is France code_insee
        return $qb->getQuery()->getOneOrNullResult();

    }
...

此方法产生以下错误:

可捕获的致命错误:参数 1 传递给 AppBundle\Form\PersonneType::AppBundle\Form{closure}() 必须是 AppBundle\Repository\NationaliteRepository 的实例, Symfony\Component\Form\Form 给定,在第 620 行的 /var/www/...vendor/symfony/symfony/src/Symfony/Component/Form/Form.php 中调用并定义

【问题讨论】:

    标签: php symfony doctrine


    【解决方案1】:

    实体/对象的最佳方法是在对象类本身上设置默认选项,无论是在实体本身还是在创建表单之前,或者使用一些构建器/工厂。

    // let say thisis the class which will be filled with the natinalite
    Class Person  {
       private $name
       // ... some other fields ...
       private $nationalite
    
       public function setNationalite(Nationalite $nationale) 
       {
          $this->nationalite = $nationalite
       }
    }
    

    表单创建对象方法

    $person = new Person();
    $person->setNationalite($repo->getNationaliteParDefaut());
    // lets say personType is your form, that also has field nationalite
    $form = $this->createForm(PersonType:class, $person);
    

    通过此设置,表单将预先填充来自对象的默认数据,包括 nationalite,并且用户仍然可以轻松更改它。

    非对象方法 另一种做你想做的事情的方法是使用表单事件。

    // in your form definition
    $builder->addEventListener(FormEvents::PRE_SET_DATA, function(FormEvent $event) {
       $form = $event->getForm();
       $data = $event->getData();
       $form->add('nationalite', EntityType::class, array(
           'class' => 'AppBundle:Nationalite',
           'choice_label' => 'libelle',
           'required' => false,
           'data' => $repo->getNationaliteParDefaut()
       )
    }
    

    关于empty_data 空数据仅在表单字段为空时才有效,例如。用户没有选择任何内容,因此将使用空数据中的值 参考:https://symfony.com/doc/current/reference/forms/types/text.html#empty-data

    【讨论】:

    • $person->setNationalite($repo->getNationaliteParDefaut()); // 在哪里/如何恢复 $repo 变量?
    • 我解决了这个问题: public function buildForm(FormBuilderInterface $builder, array $options) { $personne = $options['data']; $em = $options['entityManager']; $defaultNationalite = $em->getRepository('AppBundle:Nationalite')->getNationaliteParDefaut(); if(is_null($personne->getNationalite())){ $personne->setNationalite($defaultNationalite); } ------------- 见:symfony.com/doc/3.4/form/form_dependencies.html
    • $person->setNationalite($repo->getNationaliteParDefaut())。通常您在控制器中调用 createForm,因此您可以在那里依赖注入您的存储库或实体管理器并调用该方法。这在某种程度上是相关的:symfony.com/doc/current/forms.html#rendering-forms。在该示例之上,您只需要注入您的存储库并将数据设置到您的表单对象
    【解决方案2】:

    试试preferred_choices 选项https://symfony.com/doc/3.4/reference/forms/types/entity.html#preferred-choices。 preferred_choices 出现在列表顶部,但不会被选中。如果您想选择一个项目,请使用您想要的默认国籍的 POPO(Plain Object PHP Object)构建表单。

    【讨论】:

    • "如果你想选择一个项目,用 POPO 构建表单" //我不知道怎么做 {o_O}
    猜你喜欢
    • 2014-07-19
    • 2019-08-05
    • 2014-10-20
    • 1970-01-01
    • 2016-09-16
    • 1970-01-01
    • 2013-10-30
    • 2020-07-06
    • 2013-10-23
    相关资源
    最近更新 更多