抛开政治不谈,我需要提供Kosovo作为选择国家时的形式选择。
使用 Symfony 的内置表单选择类型执行此操作的最优雅的方法是什么country
,同时还提供翻译Kosovo name?
这是我到目前为止所做的事情,它有效,但我担心这可能有点黑客行为。
<?php
namespace Acme\Bundle\DemoBundle\Form\Type;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolverInterface;
use Symfony\Component\Intl\Intl;
class LocationType extends AbstractType
{
/**
* @param FormBuilderInterface $builder
* @param array $options
*/
public function buildForm(FormBuilderInterface $builder, array $options)
{
// Kosovo is not listed as a choice provided by the method call
// `Symfony\Component\Intl\Intl::getRegionBundle()->getCountryNames()`
// although it is mostly recognized as in independent state, including by Google Maps.
// This is because no ISO 3166-1 alpha-2 code as been assigned to that country.
// However, a temporary code 'XK' is available.
$countries = Intl::getRegionBundle()->getCountryNames();
$countries['XK'] = "Kosovo";
$builder
->add('country', 'country', array(
'choices' => $countries,
'preferred_choices' => array(
'XK', // Kosovo
'AL', // Albania
'MK', // Macedonia
'ME', // Montenegro
'GB', // United Kingdom
'US', // United States
),
))
->add('district', 'text', array('required' => false))
->add('town', 'text', array('required' => false))
;
}
}