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β
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, 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-ct-hidden-id | trigger (hide) | Space-separated field names to hide. |
data-ct-visible-id | trigger (show) | Space-separated field names to show. |
data-ct-toggle-value | trigger | Single value that toggles every target. |
data-ct-toggle-values | trigger | JSON object mapping each target field to the value that toggles it. |
data-fields-target | target field | ctHidden (for hide) or ctVisible (for show). |
Recipesβ
Hide fields while the user typesβ
form_field :title, input_type: :string,
html_attrs: { "data-action": "input->fields#hide",
"data-ct-hidden-id": "is_featured tag_ids",
"data-ct-toggle-values": '{ "is_featured": "hide", "tag_ids": "hides" }' }
form_field :is_featured, input_type: :switch, html_attrs: { "data-fields-target": "ctHidden" }
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-ct-visible-id": "is_featured tag_ids",
"data-ct-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": "ctVisible" }
Hide a whole nested form fieldβ
nested_form_field applies html_attrs to its wrapper, which already gets data-ct-id (the association name) and data-toggle-ct-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-ct-hidden-id": "addresses" }
nested_form_field :addresses, html_attrs: { "data-fields-target": "ctHidden" } do
form_field :street_address
end
Toggle a section with your own JavaScriptβ
Tag the section with html_attrs and toggle the hidden class:
section "Private Van", html_attrs: { "data-behaviour": "private_van-section" } do
form_field :transport_vehicle_number, input_type: :string
end
app/assets/javascripts/catalyst/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: reload a field's options when another field changes
- Sections and nested sections
- Progressive forms