Skip to content

collection_check_boxes hidden field ignores :form option #51606

@stimlocy

Description

@stimlocy

hidden field generated from collection_check_boxes ignores form: as html_options

Steps to reproduce

<%= form_with(url: parents_url, scope: :search, method: :get, id: 'myform') do |f| %>
  <%= f.search_field :name %>
  <%= f.submit %>
<% end %>

<%= collection_check_boxes :search, :parent_ids, Parent.all, :id, :name, {}, {form: 'myform'} %>

Expected behavior

<form id="myform" action="http://localhost:3000/parents" accept-charset="UTF-8" method="get">
  <input type="search" name="search[name]" id="search_name">
  <input type="submit" name="commit" value="Save Search" data-disable-with="Save Search">
</form>

<input form="myform" type="hidden" name="search[parent_ids][]" value="" autocomplete="off">
<input form="myform" type="checkbox" value="1" name="search[parent_ids][]" id="search_parent_ids_1">
<label for="search_parent_ids_1">Parent_1</label>

Actual behavior

<form id="myform" action="http://localhost:3000/parents" accept-charset="UTF-8" method="get">
  <input type="search" name="search[name]" id="search_name">
  <input type="submit" name="commit" value="Save Search" data-disable-with="Save Search">
</form>

<input type="hidden" name="search[parent_ids][]" value="" autocomplete="off">  <!-- not exist form="myform" -->
<input form="myform" type="checkbox" value="1" name="search[parent_ids][]" id="search_parent_ids_1">
<label for="search_parent_ids_1">Parent_1</label>

Probably the same problem as #12344

System configuration

Rails version: 7.1.3.2
Ruby version: 3.3.0

Using collection_check_boxes inside a form is fine, but if you want to use them outside of a form, it is convenient to be able to use the :form option.
For now, I am using include_hidden: false and <%= hidden_field_tag 'search[parent_ids][]', {}, form: 'myform' %> to work around the problem.

Let me know if there's any additional information needed.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions