Skip to main content

Show & Hide Fields

Hide fields and sections in two ways:

  • display_if decides on the server when the page renders. Use it for permissions or saved state.
  • The fields Stimulus controller toggles fields live as the user edits the form.

Quick example​

cm_section "Private Van", display_if: ->(trip) { trip.transport_type == "private_van" } do
form_field :transport_vehicle_number, input_type: :string, label: "Vehicle Number"
end

form_field :admin_notes, input_type: :text, display_if: ->(record) { Current.user.admin? }

Options​

display_if takes a lambda that receives the record and returns true to show. It works on row, cm_section, nested_form_section, field, form_field, column, tab, nested_form_field and alert_box. It does not re-run when the user changes a value: use the Stimulus controller for that.

Stimulus attributes, passed through html_attrs:

AttributeOnDescription
data-actiontrigger fieldinput->fields#hide or change->fields#show (any event, fields controller, hide / show action).
data-cm-hidden-idtrigger (hide)Space-separated field names to hide.
data-cm-visible-idtrigger (show)Space-separated field names to show.
data-cm-toggle-valuetriggerSingle value that toggles every target.
data-cm-toggle-valuestriggerJSON object mapping each target field to the value that toggles it.
data-fields-targettarget fieldcmHidden (for hide) or cmVisible (for show).

Recipes​

Hide fields while the user types​

form_field :title, input_type: :string,
html_attrs: { "data-action": "input->fields#hide",
"data-cm-hidden-id": "is_featured tag_ids",
"data-cm-toggle-values": '{ "is_featured": "hide", "tag_ids": "hides" }' }
form_field :is_featured, input_type: :switch, html_attrs: { "data-fields-target": "cmHidden" }

Show fields for one selected value​

form_field :user_id, input_type: :single_select, helper_method: :select_options_for_users,
html_attrs: { "data-action": "change->fields#show",
"data-cm-visible-id": "is_featured tag_ids",
"data-cm-toggle-value": ::User.first&.id.to_s }
form_field :tag_ids, input_type: :multi_select, helper_method: :select_options_for_tag,
html_attrs: { "data-fields-target": "cmVisible" }

Hide a whole nested form field​

nested_form_field applies html_attrs to its wrapper, which already gets data-cm-id (the association name) and data-toggle-cm-class="nested-field-wrapper", so only data-fields-target is needed:

form_field :has_no_previous_addresses, input_type: :switch,
html_attrs: { "data-action": "input->fields#hide", "data-cm-hidden-id": "addresses" }
nested_form_field :addresses, html_attrs: { "data-fields-target": "cmHidden" } do
form_field :street_address
end

Toggle a section with your own JavaScript​

Tag the section with html_attrs and toggle the hidden class:

cm_section "Private Van", html_attrs: { "data-behaviour": "private_van-section" } do
form_field :transport_vehicle_number, input_type: :string
end
app/assets/javascripts/cm_admin/custom.js
$(document).on("change", 'select[data-behaviour="transport_type-select"]', function (event) {
$('div[data-behaviour="private_van-section"]').toggleClass("hidden", $(event.target).val() !== "private_van");
});