OhGoogleMapFormTypeBundle with SonataAdminBundle

What should I do to make this package work with SonataAdminBundle ? I configured OhGoogleMapFormTypeBundle based on README . This is my configureFormFields method:

 protected function configureFormFields(FormMapper $formMapper) { $formMapper ->with("Map") ->add('latlng', new GoogleMapType()) ->end() ; } 

I get an error message:

 Please define a type for field `latlng` in `GM\AppBundle\Admin\PlaceAdmin` 
+4
source share
2 answers

So there really is a problem with FormMapper. The solution was quite simple, but it took a lot of time to find it. There are two ways:

First method (I didn’t like it):

 $form = new YourType(); $form->buildForm($formMapper->getFormBuilder(),array()); 

Second method:

 ->add('latlng', 'sonata_type_immutable_array',array('label' => '', 'keys' => array( array('latlng', new GoogleMapType(), array()) ))) 

Entity:

 public function setLatLng($latlng) { $this ->setLatitude($latlng['latlng']['lat']) ->setLongitude($latlng['latlng']['lng']); return $this; } /** * @Assert\NotBlank() * @OhAssert\LatLng() */ public function getLatLng() { return array('latlng' => array('lat' => $this->latitude,'lng' => $this->longitude)); } 
+8
source

First I defined GoogleMapType as a service in the app/config.yml :

 services: # ... oh.GoogleMapFormType.form.type.googlemapformtype: class: Oh\GoogleMapFormTypeBundle\Form\Type\GoogleMapType tags: - { name: form.type, alias: oh_google_maps } 

I like noob with Symfony2, so I don’t know why for some reason the alias should be oh_google_maps .

Then I set the fields and functions to store latitude and longitude in the Entity class:

 private $latlng; private $latitude; private $longitude; public function setLatlng($latlng) { $this->latlng = $latlng; $this->latitude = $latlng['lat']; $this->longitude = $latlng['lng']; return $this; } /** * @Assert\NotBlank() * @OhAssert\LatLng() */ public function getLatLng() { return array('lat' => $this->latitude,'lng' => $this->longitude); } 

Finally, in my regular Sonata Admin class, in the configureFormFields function:

 protected function configureFormFields(FormMapper $formMapper) { $formMapper //... ->add('latlng', 'oh_google_maps', array()); } 
+1
source

Source: https://habr.com/ru/post/1488462/


All Articles