I would like a <select>
element to be rendered with additional data on its <option>
s. For the sake of example, I'd like to have a service-selector (non-multiple entity-field) that resets another inputs value upon selection change. I'm not interested in using JS data-structures, I need to have the rendered field to look as follows:
<select name="...">
<option value="1" data-price="90">Service 1</option>
<option value="2" data-price="40">Service 2</option>
</select>
I would take two different solutions and would be glad to see the answer to both of them.
- I'd render the field manually in Twig by starting to assemble the above HTML code by using the
form
variable I passed to the twig. I have two problems solving this. A) I can't find a safe way to tell what the filed should be named, i.e. how do I get thename
attribute that Symfony expects by using the variableform.service
(service is the name of the field in the FormType). [Please spare me tricks that concatenate some values based on observing how fields are currently named by Symfony; one should rely on interfaces and not on reverse engineering.] B) I don't know how to access the list of choices, i.e. the array assembled by theentity
field'squery_builder
option. [Since I'm looking for a general solution, I'm not willing to duplicate these items over to a twig-parameter in the controller -- just to avoid such suggestions.] - I'd override the rendering of the relevant field blocks, as suggested in the form styling chapter of the cookbook, but there are three problems with that. A) I cannot find out which blocks should be overridden (and so I don't find samples). B) I would pass parameter from the form builder to the block to let it know what extra
data-
attributes are to be rendered, but I don't know how to do this. And finally C) in those cases where I don't need to deviate from standard rendering (e.g. when the field is multiple) I don't know how to fallback to the default rendering.
So these are actually 5 questions (1A,1B,2A,2B,2C) but I thought them to be more useful to others answered together, since they all address what I think is an undocumented spot regarding choice field rendering.