Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Form] Add option to add a class to <div class="form-check"> (radio+checkbox) #36502

Closed
ThomasLandauer opened this issue Apr 20, 2020 · 11 comments

Comments

@ThomasLandauer
Copy link
Contributor

ThomasLandauer commented Apr 20, 2020

It would be nice to have an option to add a class name here (Bootstrap theme):
https://github.com/symfony/symfony/blob/master/src/Symfony/Bridge/Twig/Resources/views/Form/bootstrap_4_layout.html.twig#L193

That is the <div> around each radio button / checkbox.

Suggested name: choice_row_attr - since choice_attr addresses the <input>, and row_attr the <div> containing all the radios.

So if you either do:

public function finishView(FormView $view, FormInterface $form, array $options)
{
    $view['field'][0]->vars['choice_row_attr'] = ['class' => 'foo'];
}

... or ...

{% for key, field in form.field.children %}
    {{ form_widget(field, {'choice_row_attr': {'class':'foo'}}) }}
{% endfor %}

...the outcome would be:

<div class="form-check foo">
  <input type="radio" ...>
</div>
<div class="form-check foo">
  <input type="radio" ...>
</div>
<div class="form-check foo">
  <input type="radio" ...>
</div>

My use case:
I'm having a list of like 20 radio buttons, and depending on the user's selection in another field, some of them get hidden (with JavaScript).
Other use case would be to highlight some radios.

@javiereguiluz
Copy link
Member

There's something I don't understand. You mentioned that choices are shown/hidden with JavaScript ... so you could add/remove/toggle those CSS classes using JavaScript, right?

@ThomasLandauer
Copy link
Contributor Author

ThomasLandauer commented Apr 20, 2020

Yeah, sure, there's a workaround: Access the <input> by its id, then go to the parent node. But this will break if Bootstrap ever adds another container in there...
And being able to just add a v-show directive (or ng-show, or whatever they have in React) directly in the right place would really be nice ;-)
And I'm sure there are other (better) use cases as well...

The general issue is that radio buttons are missing some flexibility (which other types do have), since Symfony (in principle) sees them as one field, not as a series of single widgets.

@javiereguiluz
Copy link
Member

Yes, this reminds me of the collection type, which didn't have a simple way to customize each collection item (and we made some changes in #36088 to fix that). So, we could do something similar here and allow to define attributes for each choice row. Let's ask @HeahDude what does he think about this. Thanks!

@carsonbot
Copy link

Thank you for this issue.
There has not been a lot of activity here for a while. Has this been resolved?

@xabbuh
Copy link
Member

xabbuh commented Feb 26, 2021

Given that we didn't received the feedback by more people having the need for this feature I am closing here as the desired outcome can be achieved with a custom form type extension and a custom form theme. Thank you for understanding.

@xabbuh xabbuh closed this as completed Feb 26, 2021
@VLynnik
Copy link

VLynnik commented Jun 14, 2022

The question is relevant. There is a solution ?

@flmommens
Copy link

This is required to turn a simple checkbox into a switch https://getbootstrap.com/docs/5.0/forms/checks-radios/#switches

@ryckblick
Copy link

ryckblick commented Jan 4, 2023

This is definitely a necessary feature. Came here, because I also wanted to turn a checkbox into a switch.

@derrabus
Copy link
Member

derrabus commented Jan 4, 2023

If you need it, build it and submit a PR. This project is open source.

@ryckblick
Copy link

ryckblick commented Jan 4, 2023

As this issue was closed with regards to "not enough feedback by more people" I think it is okay, to add ones feedback...
Anyhow, after some digging it seems, that this feature was actually added at one point but somehow got "lost" again. And I'm unsure how it got lost, as the Bootstrap markup didn't change in regards to switches from BS 4.3 to 5.x

Edit: Okay. The behavior is still working as described in the post I linked when enabling bootstrap_4_layout in twig.yaml but gets lost with bootstrap_5_layout

@ryckblick
Copy link

Solution: How to turn checkboxes into switches with Bootstrap 5.x
As mentioned here the option is available. But the trigger changed. Now you have to put 'checkbox-switch' in label_attr:
->add('...', CheckboxType::class, [ 'label_attr' => ['class' => 'checkbox-switch'] ])

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

8 participants