我有一个搜索表单的问题。可能我的选择列表中没有任何内容。
这就是为什么当它为空时,我想显示一个默认消息。
然而,当我设置了我的 'empty_data' 属性时,什么都没有发生,我的选择列表还是空的。
我的表单类型:
/**
* {@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';
}
在控制器层面上:
$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',
));
empty_data
是在表单前端没有选择任何选项时传回给 Symfony 的值。它本身不会在表单上显示。详见:http://symfony.com/doc/current/reference/forms/types/choice.html#empty-data - naththedeveloper$options['distributors']
中没有项目,我是否可以只放置一个占位符? - Peter Artoung