Note that there are some explanatory texts on larger screens.

plurals
  1. POCustomize the rendering of a choice/entity field in Symfony2
    primarykey
    data
    text
    <p>I would like a <code>&lt;select&gt;</code> element to be rendered with additional data on its <code>&lt;option&gt;</code>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:</p> <pre><code>&lt;select name="..."&gt; &lt;option value="1" data-price="90"&gt;Service 1&lt;/option&gt; &lt;option value="2" data-price="40"&gt;Service 2&lt;/option&gt; &lt;/select&gt; </code></pre> <p>I would take two different solutions and would be glad to see the answer to both of them.</p> <ol> <li>I'd render the field manually in Twig by starting to assemble the above HTML code by using the <code>form</code> variable I passed to the twig. I have two problems solving this. <strong>A)</strong> I can't find a safe way to tell what the filed should be named, i.e. how do I get the <code>name</code> attribute that Symfony expects by using the variable <code>form.service</code> (service is the name of the field in the FormType). [Please spare me tricks that concatenate some values based on observing how fields are <em>currently</em> named by Symfony; one should rely on interfaces and not on reverse engineering.] <strong>B)</strong> I don't know how to access the list of choices, i.e. the array assembled by the <code>entity</code> field's <code>query_builder</code> 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.]</li> <li>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. <strong>A)</strong> I cannot find out which blocks should be overridden (and so I don't find samples). <strong>B)</strong> I would pass parameter from the form builder to the block to let it know what extra <code>data-</code> attributes are to be rendered, but I don't know how to do this. And finally <strong>C)</strong> 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.</li> </ol> <p>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.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload