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​

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:

AttributeOnDescription
data-actiontrigger fieldinput->fields#hide or change->fields#show (any event, fields controller, hide / show action).
data-ct-hidden-idtrigger (hide)Space-separated field names to hide.
data-ct-visible-idtrigger (show)Space-separated field names to show.
data-ct-toggle-valuetriggerSingle value that toggles every target.
data-ct-toggle-valuestriggerJSON object mapping each target field to the value that toggles it.
data-fields-targettarget fieldctHidden (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"
);
}
);