For one of my objects I need to create some dynamic form rendering... But I cant figure out how to do this in Sonata Admin. For example when I create an object I have a field type. In this field I select a type that my object is going to be. Now when I select the type I want to make a field appear, based on the type. For example, if I select type "Carousel" I want to show a field that is selecting all object form entity Gallery. If I select type "Product" I want to display field with all products to selectt from... How can I acheve that?
Right now I have this:
/**
* @param FormMapper $formMapper
*/
protected function configureFormFields(FormMapper $formMapper)
{
$formMapper
->with('Module', array(
'class' => 'col-md-6'
))
->add('position')
->add('type', null, array(
'attr' => array('class' => 'module_type')
))
->add('items', 'entity', array(
'class' => 'ApplicationSonataMediaBundle:Gallery'
))
->end()
;
}
And I have overriden the edit template:
{% extends 'SonataAdminBundle:CRUD:edit.html.twig' %}
{% block javascripts %}
{{ parent() }}
<script type="text/javascript">
$(document).ready(function () {
$(".module_type").change(function() {
});
});
</script>
{% endblock %}
As you can see the gallery is hardcoded now..
I cant figure out how to do this now... How to say that if the value selected is this, use that entity in field... The problem is that the way form is rendered in Sonata is very complicated... I dont understand it..
maybe I should use ajax? But again, when I send a value and get the response how to add a field without refresh?
Any help appreciated.
Sonata provides you with the 'sonata_type_choice_field_mask' type which allows you to change the fields displayed on the form dynamically depending on the value of this 'sonata_type_choice_field_mask' input so you don't have to use ajax.
Here is the doc where you can find everything about sonata types and the choice field mask.
If I have realy understand your need you have to use ajax of course, first you need to add new admin route to this EntityAdminController to do it you have to override the configureRoutes method and to add your new route like this :
Then you have to create a new controller which gonna have the action definition for your new route like :
Then you have to override the
SonataAdminBundle:CRUD:edit.html.twig
template and set your javascript event listener like this :After researching forever to find a way to use dynamic dropdowns using ajax and sonata with symfony4 i would like to share my solution how it actually works for me.
In my case i have a district and this district has different cities. I have a company that first chooses a district and then depending of that it´s city.
What i did:
Create your entity for the districts
Create your entity for the cities
Go in your main class (in my case this is the company entity and add two entities for the districts and cities
Update your database schema and fill the cities and districts fields with some example data
Go into your AdminClass in the configureFormFields Function and add the following (make sure to use the 'choice_label' option correctly with the corresponding field in your district or city entity.
This should already work pretty well. You should now be able to have a dependent field. Let´s take a look to the AJAX magic now.
Go into your AdminClass (same as the configureFields-Class) and add the following
... and don´t forget to register this controller in your services.yaml...
... and finally the little template that is being called in this function...
Go into your AdminClass and insert the following code (e.g. before configureFormFields)
Now we create this cities_admin.html.twig template that overrides the default template
That´s it. Should work like a charm.
I have one question about your code :
url: "{{ admin.generateUrl('reloadCities') }}",
Tell me if I wrong, but it seems the variable
admin
is callable thank to the template extends :{% extends 'SonataAdminBundle:CRUD:edit.html.twig' %}
Only, when I extends the template, I have this error :
Any idea ?