symfony - Extending EntityType to allow extra choices set with AJAX calls -
i try create symfony custom type extending core "entity" type.
but want use select2 version 4.0.0 (ajax works "select" html element , not hidden "input" before).
- this type should create empty select instead of full list of entities extended "entity" type.
this works setting option (see configureoption):
'choices'=>array()
- by editing object attached form should populate select current data of object. solved problem view following buildview method ...
select2 recognize content of html "select", , work ajax. when form posted back, symfony doesn't recognize selected choices, (because there not allowed ?)
symfony\component\form\exception\transformationfailedexception unable reverse value property path "user": choice "28" not exist or not unique
i tried several methods using eventlisteners or subscribers can't find working configuration.
with select2 3.5.* solved problem form events , overriding hidden formtype, here extending entitytype more difficult.
how can build type let manage reverse transformation of entites ?
custom type :
<?php namespace appbundle\form\type; use symfony\component\form\abstracttype; use symfony\component\form\formbuilderinterface; use symfony\component\form\formview; use symfony\component\form\forminterface; use symfony\component\optionsresolver\optionsresolver; use symfony\component\form\choicelist\view\choiceview; class ajaxentitytype extends abstracttype { protected $router; public function __construct($router) { $this->router = $router; } /** * {@inheritdoc} */ public function buildform(formbuilderinterface $builder, array $options) { $builder->setattribute('attr',array_merge($options['attr'],array('class'=>'select2','data-ajax--url'=>$this->router->generate($options['route'])))); } /** * {@inheritdoc} */ public function buildview(formview $view, forminterface $form, array $options) { $view->vars['attr'] = $form->getconfig()->getattribute('attr'); $choices = array(); $data=$form->getdata(); if($data instanceof \doctrine\orm\persistentcollection){$data = $data->toarray();} $values=''; if($data != null){ if(is_array($data)){ foreach($data $entity){ $choices[] = new choiceview($entity->getajaxname(),$entity->getid(),$entity,array('selected'=>true)); } } else{ $choices[] = new choiceview($data->getajaxname(),$data->getid(),$data,array('selected'=>true)); } } $view->vars['choices']=$choices; } /** * {@inheritdoc} */ public function configureoptions(optionsresolver $resolver) { $resolver->setrequired(array('route')); $resolver->setdefaults(array('choices'=>array(),'choices_as_value'=>true)); } public function getparent() { return 'entity'; } public function getname() { return 'ajax_entity'; } }
parent form
<?php namespace appbundle\form; use symfony\component\form\abstracttype; use symfony\component\form\formbuilderinterface; use symfony\component\optionsresolver\optionsresolver; class alarmstype extends abstracttype { /** * @param formbuilderinterface $builder * @param array $options */ public function buildform(formbuilderinterface $builder, array $options) { $builder ->add('name','text',array('required'=>false)) ->add('user','ajax_entity',array("class"=>"appbundle:users","route"=>"ajax_users")) ->add('submit','submit'); } /** * @param optionsresolver $resolver */ public function configureoptions(optionsresolver $resolver) { $resolver->setdefaults(array('data_class' => 'appbundle\entity\alarms','validation_groups'=>array('default','form_user'))); } /** * @return string */ public function getname() { return 'alarms'; } }
problem solved.
the solution recreate form field 'choices'=>$selectedchoices in both pre_set_data , pre_submit formevents.
selected choices can retrived event $event->getdata()
have on bundle created, implements method :
Comments
Post a Comment