Search code examples
ruby-on-railsrubyvalidationtwitter-bootstrapsimple-form

Showing fields with errors for nested forms in Rails 3.2 + SimpleForm


I have a Flight model nested inside a FlightLog model. A FlightLog may contain many flights.

I'm using SimpleForm with the bootstrap installation, which makes it possible to surround form elements with errors with the error class when a validation fails.

The problem is, that even though validations are triggered for the nested model, the fields with errors inside the simple_fields_for are not being marked, so it's not possible to determine which attribute is not valid.

After examining the errors hash when calling the create action, I can see that it is correctly populated with the errors at the top level, and the errors of the nested resources inside each resource.

How could I modify the behavior of simple_form to add the errors class to the control group of each nested model to match the behavior of the parent?

Thanks in advance.

enter image description here


Solution

  • I have been using custom accessors instead of the _id fields, so that's why they weren't getting notified when they had errors. I finally resolved to use f.error :attr_name under each accessor and changing the styling manually with JS