I'm trying to implement dynamic forms in Symfony2. The form is simple, first the user selects a broker, and then select an account according to that broker. Therefore, the account is disabled until the broker is selected.
In case of a text input, read_only => true could be a solution, but select has no attribute readonly. So, I made the select disabled, and then tried to enable the select with jQuery:
$('#form select').prop('disabled', false);
The problem is that on the server side, Symfony cannot handle the disabled form field. Its value is simply set to null.
I thought of possible solutions. First of all, I could manually set the field value in the controller, because I tested that the form field value is actually in the request parameter, Symfony just ignores to handle it and map it to the object. I think it would work, however it's not really nice solution.
Secondly, I can disable the select with some client side hacking, or add a hidden input to the form and manually set the value of it with Javascript.
Is there a standard solution here?
My form entity looks like this:
$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 disable the select, everything works fine, Symfony handles the request perfectly.