<p dir="auto">I am running Symfony 4.2 and trying to create two Bootstrap 3 radio buttons.  Here is the HTML for what I want to create:</p>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="&lt;div class=&quot;btn-group btn-group-justified&quot; data-toggle=&quot;buttons&quot;&gt;
    &lt;label class=&quot;btn btn-lg btn-default&quot;&gt;
        &lt;input type=&quot;radio&quot; name=&quot;inquiry[flexDates]&quot; value=&quot;yes&quot;&gt;YES
    &lt;/label&gt;
    &lt;label class=&quot;btn btn-lg btn-default&quot;&gt;
        &lt;input type=&quot;radio&quot; name=&quot;inquiry[flexDates]&quot; value=&quot;no&quot;&gt;NO
    &lt;/label&gt;
&lt;/div&gt;"><pre class="notranslate"><code class="notranslate">&lt;div class="btn-group btn-group-justified" data-toggle="buttons"&gt;
    &lt;label class="btn btn-lg btn-default"&gt;
        &lt;input type="radio" name="inquiry[flexDates]" value="yes"&gt;YES
    &lt;/label&gt;
    &lt;label class="btn btn-lg btn-default"&gt;
        &lt;input type="radio" name="inquiry[flexDates]" value="no"&gt;NO
    &lt;/label&gt;
&lt;/div&gt;
</code></pre></div>
<p dir="auto">This is my form builder code:</p>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="$builder
    -&gt;add('flexDates', ChoiceType::class, [
        'choices' =&gt; [
            'Yes' =&gt; 'yes',
            'No'  =&gt; 'no',
         ],
         'required' =&gt; false,
         'empty_data' =&gt; '',
         'expanded' =&gt; true,  // these two options (expanded = true and
         'multiple' =&gt; false, // multiple = false) make it a radio button
     ])"><pre class="notranslate"><code class="notranslate">$builder
    -&gt;add('flexDates', ChoiceType::class, [
        'choices' =&gt; [
            'Yes' =&gt; 'yes',
            'No'  =&gt; 'no',
         ],
         'required' =&gt; false,
         'empty_data' =&gt; '',
         'expanded' =&gt; true,  // these two options (expanded = true and
         'multiple' =&gt; false, // multiple = false) make it a radio button
     ])
</code></pre></div>
<p dir="auto">I have the Bootstrap 3 theme (<code class="notranslate">bootstrap_3_layout.html.twig</code>) installed and have tried various combinations of <code class="notranslate">form_row()</code>, <code class="notranslate">form_label()</code> and <code class="notranslate">form_widget()</code>.  I've set <code class="notranslate">attr</code> and <code class="notranslate">label_attr</code> in several different ways,<br>
but I cannot figure out how to generate the HTML above.  There always seem to be extra <code class="notranslate">&lt;div&gt;</code>s or HTML that causes it to display incorrectly.</p>
<p dir="auto">The HTML is part of a hard-coded form that has been working fine and I'm trying to convert it to a Twig template.  Converting all the other fields was simple, but I've wasted nearly a day just trying to duplicate the radio buttons.</p>
<p dir="auto">It seems like it should be easy, but how to do this is not clear from any of the documentation or examples I've been able to find.</p>
<p dir="auto">I've tried creating my own block widget, but I can't figure out how to make the <code class="notranslate">&lt;label&gt;</code> include the <code class="notranslate">&lt;input&gt;</code> (instead of before or after).  In order to make things work, the label must wrap around the input.</p>
<p dir="auto">There is a <code class="notranslate">{% block checkbox_radio_label %}</code> defined in the <code class="notranslate">bootstrap_3_layout.html.twig</code> file that is close to what I need, but I cannot get it to work.  It uses <code class="notranslate">{{ widget|raw }}</code>, but Symfony gives me a <code class="notranslate">variable "widget" does not exist</code> error when I try to use it.</p>
<p dir="auto">So far, the information at <a href="https://symfony.com/doc/current/form/form_customization.html" rel="nofollow">https://symfony.com/doc/current/form/form_customization.html</a> has been more confusing than helpful.  I've been using Twig for years and have been able to do almost anything else, but this problem has me stumped.  I know I could write my own custom filter, but it seems like there should be an easier way.</p>
<p dir="auto">Working on some internal code, I was having trouble understanding certain error messages. The main reason was because I wrote a "parameter" instead of "parameters". To save others some time, I wanted to show a nice possible alternative value that <em>is</em> allowed instead. I know that Symfony has a lot of those messages and they add value to the Developer Experience.</p>
<p dir="auto">So I wanted to implement this and it occurred to me how often there was code duplication! I've done a very basic search for <a href="https://github.com/symfony/symfony/search?utf8=%E2%9C%93&amp;q=Did+you+mean">"Did you mean"</a> and got 2 pages of which not all contained the actual implementation. There's some <a href="https://github.com/symfony/symfony/search?utf8=%E2%9C%93&amp;q=alternatives&amp;type=Code">additional implementations</a> to be found all around the code base but all come down to 1 thing, suggesting alternatives.</p>
<p dir="auto">I think it would be a great addition to have a generic way to not only generate alternative suggestions within Symfony, but also for people who want to implement the DX concept in their own applications or libraries. I would imagine this to be a tiny component compared to others which could (for now) provide the following:</p>
<div class="highlight highlight-text-html-php notranslate position-relative overflow-auto" dir="auto" data-snippet-clipboard-copy-content="// just an example, no idea how this would be named or if both cases are needed
use Symfony\Component\Alternative\ManyAlternatives;
use Symfony\Component\Alternative\SingleAlternative;

$possibleOptions = ['foo', 'foobar', 'bar'];

echo implode(', ', ManyAlternatives::match(['fo'], $possibleOptions));
// foo, foobar

echo SingleAlternative::match(['fo'], $possibleOptions);
// foo

// or optionally with a custom value resolving
echo ManyAlternatives::match(
    new Definition('foo'),
    $possibleOptions, 
    function (Definition $d) { return $d-&gt;getName(); }
);"><pre class="notranslate"><span class="pl-c">// just an example, no idea how this would be named or if both cases are needed</span>
<span class="pl-k">use</span> <span class="pl-v">Symfony</span>\<span class="pl-v">Component</span>\<span class="pl-v">Alternative</span>\<span class="pl-v">ManyAlternatives</span>;
<span class="pl-k">use</span> <span class="pl-v">Symfony</span>\<span class="pl-v">Component</span>\<span class="pl-v">Alternative</span>\<span class="pl-v">SingleAlternative</span>;

<span class="pl-s1"><span class="pl-c1">$</span>possibleOptions</span> = [<span class="pl-s">'foo'</span>, <span class="pl-s">'foobar'</span>, <span class="pl-s">'bar'</span>];

<span class="pl-k">echo</span> implode(<span class="pl-s">', '</span>, <span class="pl-v">ManyAlternatives</span>::<span class="pl-en">match</span>([<span class="pl-s">'fo'</span>], <span class="pl-s1"><span class="pl-c1">$</span>possibleOptions</span>));
<span class="pl-c">// foo, foobar</span>

<span class="pl-k">echo</span> <span class="pl-v">SingleAlternative</span>::<span class="pl-en">match</span>([<span class="pl-s">'fo'</span>], <span class="pl-s1"><span class="pl-c1">$</span>possibleOptions</span>);
<span class="pl-c">// foo</span>

<span class="pl-c">// or optionally with a custom value resolving</span>
<span class="pl-k">echo</span> <span class="pl-v">ManyAlternatives</span>::<span class="pl-en">match</span>(
    <span class="pl-k">new</span> <span class="pl-v">Definition</span>(<span class="pl-s">'foo'</span>),
    <span class="pl-s1"><span class="pl-c1">$</span>possibleOptions</span>, 
    <span class="pl-k">function</span> (<span class="pl-smi"><span class="pl-smi">Definition</span></span> <span class="pl-s1"><span class="pl-c1">$</span>d</span>) { <span class="pl-k">return</span> <span class="pl-s1"><span class="pl-c1">$</span>d</span>-&gt;<span class="pl-en">getName</span>(); }
);</pre></div>