ajax - Create form in Controller using Sonata field type -


in symfony admin have form, second field type depends on choice field value selected. second field can of symfony url field type or sonata provided sonata_type_model_list field type.

i have created ajax request bundle controller return form, contains needed field.

> /src/mybundle/controller/mycontroller.php  namespace mybundle\controller  use sonata\adminbundle\controller\crudcontroller; use symfony\component\httpfoundation\request; use doctrine\orm\mapping\classmetadatainfo; use sonata\adminbundle\form\formmapper;  class mycontroller extends crudcontroller {     public function getfieldaction()   {     //getting value of choice field     $type = $this->get('request')->get('type');       //sonata.admin.reference service name of referencebundle admin class         $fielddescription = $this->admin->getmodelmanager()          ->getnewfielddescriptioninstance($this->admin->getclass(), 'reference');         $fielddescription->setassociationadmin($this->container->get('sonata.admin.reference'));         $fielddescription->setadmin($this->admin);         $fielddescription->setassociationmapping(array(             'fieldname' => 'reference',             'type' => classmetadatainfo::one_to_many,         ));      // getting form mapper in controller:     $contractor = $this->container->get('sonata.admin.builder.orm_form');     $mapper = new formmapper($contractor, $this->admin->getformbuilder(), $this->admin);      $form_mapper = $mapper->add('reference', 'sonata_type_model_list', array(             'translation_domain' => 'referencebundle',             'sonata_field_description' => $fielddescription,             'class' => $this->container->get('sonata.admin.reference')->getclass(),             'model_manager' => $this->container->get('sonata.admin.reference')->getmodelmanager(),             'label' => 'reference',             'required' => false,         ));       //@todo build $form $form_mapper       return $this->render('mybundle:form:field.view.html.twig', array(         'form' => $form->createview(),     ));   } } 

i cannot find method in sonata\adminbundle\form\formmapper class build form (it seems possible create() method, works common symfony field types, not sonata form field types, commonly generated in block or admin classes).

is possible use sonata\adminbundle\form\formmapper in controller build form? or there way can build form sonata form field types in controller?

you should not use controller, rather sonata admin service.

sonata provides 'sonata_type_choice_field_mask' type allows change fields displayed on form dynamically depending on value of 'sonata_type_choice_field_mask' input.

here doc can find sonata types , choice field mask.

protected function configureformfields(formmapper $formmapper) {     $formmapper         ->add('reference', 'sonata_type_choice_field_mask', array(             'choices' => array(                 //the list of available 'reference' here                 'choice1',                 'choice2'             ),             'map' => array(                 //what want display depending of selected option                 'choice1' => array(                     // list of fields displayed if choice 1 selected                     'field1', 'field3'                 ),                 'choice2' => array(                     // list of fields displayed if choice 2 selected                     'field2', 'field3'                 )             ),             'placeholder' => 'choose option',             'required' => false         ))         ->add('field1', 'sonata_type_model_list', array(/* options goes here */))         ->add('field2', 'url', array(/* options goes here */))         ->add('field3')     ; } 

Comments

Popular posts from this blog

sublimetext3 - what keyboard shortcut is to comment/uncomment for this script tag in sublime -

java - No use of nillable="0" in SOAP Webservice -

ubuntu - Laravel 5.2 quickstart guide gives Not Found Error -