Checkbox

Enhanced checkbox field with consistent styling, hint support, and improved accessibility.

Usage

<%= form_with url: root_path do |f| %>
  <%= f.check_box :newsletter %>
  <%= f.check_box :terms, checked: true %>
  <%= f.check_boxes_collection(:country_id, countries, :id, :name) %>
<% end %>
Props
PropTypeDefaultDescription
labelString | falsehumanized attribute nameCustom label text. Set to `false` to hide the label.
hintStringnilHelper text displayed below the checkbox.
errorStringnilManual error message. ActiveRecord errors are displayed automatically.
checkedBooleanfalsePre-check the checkbox.
disabledBooleanfalseDisable the checkbox.

Also accepts any HTML attributes via **options (e.g., id:, data:, aria:). class: is also supported for custom styling.

Examples

Basic Checkbox

<%= form_with url: root_path do |f| %>
  <%= f.group class: "form__checkbox_collection" do %>
    <%= f.check_box :newsletter %>
    <%= f.check_box :free_shipping, checked: true %>
  <% end %>
<% end %>

CheckBox Collection

<%= form_with url: root_path do |f| %>
  <%= f.check_boxes_collection(:country_id, ISO3166::Country.find_all_countries_by_subregion("Northern America"), :alpha2, :common_name) %>
<% end %>

Label customization

<%= form_with url: root_path do |f| %>
  <%= f.group class: "form__checkbox_collection" do %>
    <%= f.check_box :custom_label, label: "Custom Label Text" %>
    <%= f.check_box :with_link, label: "I agree to the #{link_to 'terms and conditions', '#', class: 'link'}".html_safe %>
    <%= f.check_box :default_label %>
  <% end %>
<% end %>

Hint messages

Receive marketing emails

<%= form_with url: root_path do |f| %>
  <%= f.check_box :receive_mails, hint: "Receive marketing emails" %>
<% end %>

Disabled State

<%= form_with url: root_path do |f| %>
  <%= f.group class: "form__checkbox_collection" do %>
    <%= f.check_box :disabled_checkbox, disabled: true %>
    <%= f.check_box :disabled_checkbox_checked, checked: true, disabled: true %>
  <% end %>
<% end %>

Validation

Is required

Custom error

<% user = User.new %>
<% user.errors.add(:terms_and_conditions, "is required") %>
<%= form_with model: user, url: root_path do |f| %>
  <%= f.check_box :terms_and_conditions, label: "I agree to the terms and conditions" %>
  <%= f.check_box :custom_error, error: "Custom error" %>
<% end %>