bootstrap_form icon indicating copy to clipboard operation
bootstrap_form copied to clipboard

Extra markup generated: multiple="multiple" from `collection_check_boxes`

Open lcreid opened this issue 7 years ago • 7 comments

The tests check for an attribute multiple="multiple" on the hidden input field generated at the start of a group of check boxes. The HTML spec on the multiple attribute doesn't appear to rule it out, but MDN doesn't show it being used on check boxes, and W3Schools explicitly says it's only for file and email input types and the select tag.

Should this be cleaned up?

lcreid avatar Feb 01 '18 20:02 lcreid

What rails version does that? Looking at current master options[:multiple] should not propagate to the hidden field. See: https://github.com/rails/rails/blob/20c91119903f70eb19aed33fe78417789dbf070f/actionview/lib/action_view/helpers/tags/check_box.rb#L23

Also, is it even possible to bootstrap wrap collection_check_boxes helper? There's a lot of markup around and logic that needs to be added for errors.

GBH avatar Feb 02 '18 00:02 GBH

I haven't look into this specifically, but there are a lot of places in the bootstrap_form code where it can be easy to forget to delete and option, which then gets passed through to the underlying Rails helper, and I believe the default action in the Rails helpers is to output any unrecognized options as an attribute.

The current BootstrapForm::FormBuilder#collection_check_boxes and BootstrapForm::FormBuilder#collection_radio_buttons don't, in fact, wrap the Rails helpers. I've been meaning to put in an issue for that...

lcreid avatar Feb 02 '18 00:02 lcreid

Oh yeah. They are just being overridden. That makes sense.

About leaking options I raised issue here: https://github.com/bootstrap-ruby/bootstrap_form/issues/386

GBH avatar Feb 02 '18 00:02 GBH

I finally understand what the issue is.

hidden_field(method, multiple: true, value: "")

This will append [] to the name attribute and still will generate multiple="multiple" attribute. It seems intentional yet completely undocumented.

I guess to side-step that would be to construct hidden_field_tag manually. Unfortunately code to construct the name attribute is way too deep and I can't figure out how to access it. Probably not worth it.

GBH avatar Feb 06 '18 03:02 GBH

FWIW: The Rails helper deletes the multiple options once it generates the appropriate attributes, so it doesn't render a multiple attribute: https://github.com/rails/rails/blob/375a4143cf5caeb6159b338be824903edfd62836/actionview/lib/action_view/helpers/tags/check_box.rb#L25.

lcreid avatar Jul 24 '18 15:07 lcreid

Code like this still generates a hidden field passed on as a parameter like "categories"=>["", "1"], , which prevents the form to be saved and causes an error: <%= f.collection_check_boxes :categories, Category.all, :id, :title %>

Adding include_hidden: false doesn't fix the issue. What is an elegant way to go about fixing this?

iraszl avatar Mar 17 '19 06:03 iraszl

Apologies that I haven't responded sooner. I agree that bootstrap_form should be able to handle this elegantly, but the last time I had a chance to look, it wasn't going to be easy. I'll try to make some time to look at it again. Thanks for your input!

lcreid avatar Mar 25 '19 15:03 lcreid

I am looking at this now. We just want to remove the multiple="multiple" attribute, right?

donv avatar Sep 08 '23 10:09 donv