I am working on a program built with the Sonata Admin bundle. I have a ScheduleAdmin class that includes the following logic:
$formMapper
->add(
'markets',
null,
[
'required' => true,
'label' => 'shared_countries',
'data' => $this->getMarketsByUser($this->getUser())
]
)
;
The relationship between Schedules and Markets is a many-to-many relationship.
The getMarketsByUser()
method always returns one market -- USA
for example. What I get in my browser is a field that includes my market, but also includes an autofill dropdown containing every other country.
Now I want to get rid of those other autofill options, allowing only the market affiliated with my user. How do I do that?
Well that was simple. I took a look at this page and then changed my add()
method call to look like this:
$formMapper
->add(
'markets',
null,
[
'required' => true,
'label' => 'shared_countries',
'data' => $this->getMarketsByUser($this->getUser()),
'choices' => $this->getMarketsByUser($this->getUser()),
]
)
;
... and now it works swell.