douyin2435 2017-06-27 14:29
浏览 43
已采纳

Symfony 3 ChoiceType - 忽略属性empty_data

I have a problem with my search form. It is possible that my select contains nothing. This is why I would like to display a default message when it is empty.

However when I set my 'empty_data' attribute, nothing happens, always my empty select. My FormType :

/**
     * {@inheritdoc}
     */
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder->add('organisation',ChoiceType::class,array(
            'choices' => $options['distributeurs'],
            'choice_label' => function ($value, $key, $index) {
                    return $value->getOrganisation();
                },
            'choice_value' => 'id',
            'empty_data' => 'No distributor found'
        ));
    }

    /**
     * {@inheritdoc}
     */
    public function configureOptions(OptionsResolver $resolver)
    {
        $resolver->setDefaults(array(
            'distributeurs' => 'ProjectBundle\Entity\Organisation\Organisation',
        ));
    }

    /**
     * {@inheritdoc}
     */
    public function getBlockPrefix()
    {
        return 'projectbundle_organisation_search';
    }

emptySelect

And at the level of the controller :

 $distributeurs = $em->getRepository('ProjectBundle:Organisation\Organisation')->createQueryBuilder('a')
            ->where('a.id IN (:distributeurs)')->setParameter('distributeurs',$organisationDistriId)->getQuery()->execute();
        $form = $this->createForm('ProjectBundle\Form\Organisation\OrganisationDistributeurType', null, array(
            'distributeurs' => $distributeurs,
            'action' => $this->generateUrl('admin_organisations_index'),
            'method' => 'GET',
        ));

The form and information work correctly, there is just the 'empty_data' attribute that is not displayed.

The 'placeholder' attribute works but that's not what I want.

Do you have an idea ?

Thanks !

  • 写回答

1条回答 默认 最新

  • doulu6234 2017-06-27 14:41
    关注

    Try to post your code next time and not a picture because that is not clear.
    In your Form Builder try to create the select in this way (Placeholder is implemented from version 2.6):

    $builder->add('organisation', ChoiceType::class, array(
    'placeholder' => 'Choose an option',
    ));
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 用Multisim设计汽车尾灯控制电路
  • ¥15 关于#java#的问题,请各位专家解答!(开发工具-eclipse)
  • ¥100 求用matlab求解上述微分方程的程序代码
  • ¥15 请问各位,如何在Jetson nano主控板的Ubuntu系统中安装PyQt5
  • ¥15 MAC安装佳能LBP2900驱动的网盘提取码
  • ¥400 微信停车小程序谁懂的来
  • ¥15 ATAC测序到底用什么peak文件做Diffbind差异分析
  • ¥15 安装ubantu过程中第一个vfat 文件挂载失败
  • ¥20 GZ::CTF如何兼容一些靶机?
  • ¥15 etcd集群部署问题