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`
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));
}
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());
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With