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

Unwanted hidden input on check_box helper with bootstrap_form_tag #215

Closed
soullivaneuh opened this issue Apr 2, 2015 · 8 comments · Fixed by #627
Closed

Unwanted hidden input on check_box helper with bootstrap_form_tag #215

soullivaneuh opened this issue Apr 2, 2015 · 8 comments · Fixed by #627

Comments

@soullivaneuh
Copy link

With the following template:

= bootstrap_form_tag url: admin_namespaces_projects_path, method: :get do |f|
  = f.form_group label: { text: 'Activity' } do
    = f.check_box :with_push, name: :with_push, label: 'Projects with push events', checked: params[:with_push]

We got this html result for the with_push element:

<div class="checkbox">
  <label for="_with_push">
    <input name="with_push" type="hidden" value="0">
    <input id="_with_push" name="with_push" type="checkbox" value="1">
    Projects with push events
  </label>
</div>

The hidden field is unwanted and produce this following url parameters:

&with_push=0&with_push=1

That not works.

Is that an issue? How to disable it?

Thanks.

@soullivaneuh
Copy link
Author

More weird:

= f.form_group label: { text: 'Visibility level' } do
  - Project.visibility_levels.each do |label, level|
    = f.check_box :visibility_levels, name: 'visibility_levels[]', label: label, value: level, checked: params[:visibility_levels].present? && params[:visibility_levels].include?(level.to_s)

Produce:

<div class="form-group">
  <label class="control-label">
    Visibility level
  </label>
  <div class="checkbox">
    <label for="_visibility_levels">
      <input name="visibility_levels[]" type="hidden" value="0">
      <input id="_visibility_levels" name="visibility_levels[]" type="checkbox" value="1">
      Private
    </label>
  </div>
  <div class="checkbox">
    <label for="_visibility_levels">
      <input name="visibility_levels[]" type="hidden" value="0">
      <input id="_visibility_levels" name="visibility_levels[]" type="checkbox" value="1">
      Internal
    </label>
  </div>
  <div class="checkbox">
    <label for="_visibility_levels">
      <input name="visibility_levels[]" type="hidden" value="0">
      <input id="_visibility_levels" name="visibility_levels[]" type="checkbox" value="1">
      Public
    </label>
  </div>
</div>

@datWav
Copy link

datWav commented Apr 24, 2015

The hidden input is needed. If you remove the hidden field and uncheck the checkbox the value are still true after the save.
Please check: http://stackoverflow.com/questions/11550941/why-does-the-check-box-form-helper-generate-two-checkboxes-one-hidden

@hut8
Copy link

hut8 commented Sep 18, 2015

This causes issues in collections. I have a filter which has some checkboxes. If none are checked, it should display every record. But since model[my_filter][]='' ends up in the query string, the SQL generated by passing params[:model] to where ends up matching nothing.

In Rails 4 with form_for, there's a way to disable this by passing include_hidden: false as an option to the collection_check_boxes method (in actionview/lib/action_view/helpers/tags/collection_check_boxes.rb). The main reason I'm posting this is because it seems like that doesn't work with bootstrap_form_for. If I change my code to just use form_for, with the option I mentioned passed to the collection_check_boxes method, everything works.

@sinscary
Copy link

@hut8 Nope, this is not working for me. I am still getting hidden field, even after using include_hidden: false

@krsyoung
Copy link
Contributor

I'm having a hard time finding a clean way to work around this (Google is not giving any answers!). The issue seems to be with: https://github.com/bootstrap-ruby/rails-bootstrap-forms/blob/0fba8444cecdccec3c662de3988cc2d35c3fe2e5/lib/bootstrap_form/form_builder.rb#L168-L174

The hidden_field is always appended (regardless of any include_hidden: false) setting. A (ugly) workaround could be something like:

    def collection_check_boxes_with_bootstrap(*args)
      include_hidden = true
      html = inputs_collection(*args) do |name, value, options|
        options[:multiple] = true
         if options.key?(:include_hidden) && options[:include_hidden] == false
           include_hidden = false
         end
        check_box(name, options, value, nil)
      end
      if include_hidden
        html = hidden_field(args.first,{value: "", multiple: true}).concat(html)
      end
      html
    end

This then ensures the include_hidden: false is respected. Would love to find a good example to work around this without touching include_hidden at all.

@mattbrictson
Copy link
Contributor

Cleaning up old unanswered issues. If this question is still relevant, please reopen.

@a5-stable
Copy link
Contributor

a5-stable commented Jan 12, 2022

Hi, Is there any progress on this?
I'm in trouble with it.

@donv
Copy link
Collaborator

donv commented Jan 14, 2022

I looked at the PR, and it looks good to me. However there is a problem with the "danger" linting, and I am not familiar with it.

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

Successfully merging a pull request may close this issue.

8 participants