TrestleAdmin / trestle

A modern, responsive admin framework for Ruby on Rails
https://trestle.io
GNU Lesser General Public License v3.0
1.94k stars 173 forks source link

Errors on check_box fields not displaying #455

Closed acoffman closed 8 months ago

acoffman commented 8 months ago

As an example, I have the following validation set up in my model:

record.errors.add :deprecated, "Error message"

deprecated is a boolean field on the ActiveRecord model.

I have the following in my form:

col(sm: 2) { check_box :deprecated }

If I check the box, the validation runs as expected, attaches the error, and the save fails, but the error message is not displayed on the form field. Just the generic flash "please correct the errors below" banner.

I also have

col(sm: 2) { text_field :name }

in the same form. If I change the validation to attach the error to name instead:

record.errors.add :name, "Error message"

it correctly appears next to the name input.

Is there something specific I need to do to have the check_box helper display validation errors as expected?

spohlenz commented 8 months ago

Most fields like text_fields are automatically wrapped in a form_group (which creates the wrapper div, label and errors) by the Trestle form builder. check_box fields are a special case where they aren't automatically wrapped (primarily due to there being a redundant label by default, and misaligned fields if the label is dropped).

To restore the error fields, wrap the check_box in a form_group, optionally specifying label: false or an alternate label (e.g. 'Deprecation Options').

form_group :deprecated, label: false do
  check_box :deprecated
end
acoffman commented 8 months ago

Thanks so much, I figured I was just missing something straightforward like that!