Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

OhGoogleMapFormTypeBundle with SonataAdminBundle

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

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

I'm getting error:

Please define a type for field `latlng` in `GM\AppBundle\Admin\PlaceAdmin`
like image 731
winnfield Avatar asked Jan 13 '23 01:01

winnfield


2 Answers

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

The first method (I did not like):

$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));
}
like image 185
Jura Avatar answered Jan 16 '23 21:01

Jura


I first defined the GoogleMapType as a service, in the app/config.yml file:

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

I'm kinda noob with Symfony2, so I don't know why by some reason the alias must be oh_google_maps.

Then, I set up the fields and functions for storing the latitude and longitude in my 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 custom Sonata Admin class, at the configureFormFields function:

protected function configureFormFields(FormMapper $formMapper)
{
    $formMapper
        //...
        ->add('latlng', 'oh_google_maps', array());
}
like image 26
Guillermo Gutiérrez Avatar answered Jan 16 '23 22:01

Guillermo Gutiérrez