0

So I was using symfony for a while but there is problem that, each time I face it, I get very angry, seriously :).
Let's assume I have an entity Client with OneToMany relation with entity Address, and let's say I have a form with two selects: Clients and Addresses, so I select a client and load it's addresses in the select below dynamically using ajax, as described in the picture below:
enter image description here After sending the form with selected client and address symfony gives me this error about the addresses select:
This value is not valid
This is my builder that gave me this error:

$builder
->add('client', EntityType::class, array(
                'class' => Client::class,

      ))
->add('addresses', EntityType::class, array(
                'class' => Address::class,
                'choices' => array(), //---To force symfony to not launch a query to get all addresses
      ))

When I remove the line 'choices' => array() the form works fine and send the data correctly.
I really need to solve this issue because I need something like that a lot in my application that I'm now working on it.

SlimenTN
  • 3,383
  • 7
  • 31
  • 78

1 Answers1

0

With this code, is like you tell Symfony to accept not thing.

'choices' => array()

Apparently in your app the choice of address depends on each client. So the solution is to use Form Events to Dynamically Modify your forms. Here a good example from the Symfony official document. Dynamic Generation for Submitted Forms

Omar Ghorbel
  • 680
  • 5
  • 12