我有一个由6个字段组成的Location对象.其中一些字段是可选的.
所以我有一个LocationSelectType,它根据位置,PRE_SET_DATA和PRE_SUBMIT事件填充字段.这工作正常.
但是在PRE_SUBMIT上,我还想根据用户输入的数据创建Location对象.这似乎有效,但最后会触发错误:*transformationFailure"复合形式在提交时需要数组或NULL."*
class LocationSelectType extends AbstractType {
public $em;
private $router;
private $options;
public function __construct(EntityManager $em,Router $router)
{
$this->em = $em;
$this->router = $router;
}
/**
* @param FormBuilderInterface $builder
* @param array $options
*/
public function buildForm(FormBuilderInterface $builder, array $options)
{
$em = $this->em;
$builder
->add('country','choice',array(
'choices'=>$this->em->getRepository('MyWorldBundle:Country')->findCountryList();,
'required'=>false,
'mapped'=>false,
'empty_value'=>'Votre pays',
'attr'=>array('class'=>'geo-select geo-select-country geo-select-ajax','data-geo-level'=>'country','data-icon'=>'globe','data-ajax-url'=>$options['ajax_url'],'style'=>"width:100%"),
))
->add('region','choice',array(
'choices'=>array(), //populate on events
'required'=>false,
'mapped'=>false,
'empty_value'=>'Votre région',
'attr'=>array('class'=>'geo-select geo-select-region geo-select-ajax hide','data-geo-level'=>'region','data-icon'=>'globe','data-ajax-url'=>$options['ajax_url'],'style'=>"width:100%"),
))
->add('departement','choice',array(
'choices'=>array(), //populate on events
'required'=>false,
'mapped'=>false,
'empty_value'=>'Votre …Run Code Online (Sandbox Code Playgroud)