Show & Hide Fields
Hide fields and sections in two ways:
display_ifdecides on the server when the page renders. Use it for permissions or saved state.- The
fieldsStimulus 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:
| Attribute | On | Description |
|---|---|---|
data-action | trigger field | input->fields#hide or change->fields#show (any event, fields controller, hide / show action). |
data-cm-hidden-id | trigger (hide) | Space-separated field names to hide. |
data-cm-visible-id | trigger (show) | Space-separated field names to show. |
data-cm-toggle-value | trigger | Single value that toggles every target. |
data-cm-toggle-values | trigger | JSON object mapping each target field to the value that toggles it. |
data-fields-target | target field | cmHidden (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");
});
Relatedβ
- Select options:
toggle_visibilityfrom a linked field - Sections and nested sections
- Progressive forms