I am trying to implement dynamic forms in Symfony2. The form is simple: first, the user selects a broker, and then selects an account in accordance with this broker. Therefore, the account is disabled until a broker is selected.
If text is read_only => true may be the solution, but select does not have the readonly attribute. So, I disabled selection, and then tried to enable select using jQuery:
$('#form select').prop('disabled', false);
The problem is that on the server side, Symfony cannot handle a disabled form field. Its value is simply set to zero.
I was thinking about possible solutions. First of all, I could manually set the field value in the controller, because I tested that the value of the form field is actually in the request parameter, Symfony simply ignores its processing and matches it to the object. I think this will work, however this is not a good solution.
Secondly, I can turn off the selection using hacking on the client side or add hidden input to the form and manually set its value using Javascript.
Is there a standard solution here?
My form object is as follows:
$builder->add('broker', 'entity', array( 'class' => 'MyBundle:Broker', 'property' => 'name', 'query_builder' => function(BrokerRepository $br) { return $br->getActiveBrokersQuery(); }, 'required' => true, 'empty_value' => 'Choose a broker', )); $builder->add('accountType', 'entity', array( 'class' => 'MyBundle:AccountType', 'empty_value' => 'Choose a broker first', 'disabled' => true, ));
If I do not turn off the selection, everything will be fine, Symfony will do the job fine.