为了获得一个HTML select标记,其中只包含我想要显示的国家,并通过
Symfony国际组件
(
see doc here
)当我的网站的区域设置发生变化时,我创建了一个
国
实体,带有自定义getter
getTranslatedName()
通过ISO代码检索翻译后的名称。
然后在我的
联系人类型
实体类型
为了国家。
它工作正常,但是当您更改区域设置时,国家不会按字母顺序排序(默认区域设置为英语)。
/**
* @return null|string
*/
public function getCountryName()
{
if (null === $this->getIso()) {
return $this->getName();
}
return Intl::getRegionBundle()->getCountryName($this->getIso());
}
我的实体类型:
->add('country', EntityType::class, array(
'class' => Country::class,
'query_builder' => function (EntityRepository $er) {
return $er->createQueryBuilder('c')
->where('c.cc IS NOT NULL')
->orderBy('c.name', 'ASC');
},
'choice_label' => 'countryName',
'choices_as_values' => true,
'data' => $options['country'],
'required' => true,
'placeholder' => 'Choose from the list',
'label' => 'Country'
))