5

When saving a form and a field fails validation, I don't want the label to be modified at all. I don't mind wrapping the input with a div and a class of "field_with_errors", but how can I stop it from wrapping the label as well?

Tom Rossi
  • 11,604
  • 5
  • 65
  • 96
  • Does this answer your question? [Rails 3: "field-with-errors" wrapper changes the page appearance. How to avoid this?](https://stackoverflow.com/questions/5267998/rails-3-field-with-errors-wrapper-changes-the-page-appearance-how-to-avoid-t) – Rajkaran Mishra Nov 01 '19 at 15:00

2 Answers2

7

I wasn't able to find anything in the ActionView::Base documentation about the field_error_proc method, but here is the solution I came up with. Hopefully this will help others!

 ActionView::Base.field_error_proc = Proc.new do |html_tag, instance| 
  unless html_tag =~ /^<label/
    "<div class=\"field_with_errors\">#{html_tag}</div>".html_safe
  else
    html_tag.html_safe
  end
end
Tom Rossi
  • 11,604
  • 5
  • 65
  • 96
-1

Instead of having for example

<%=label "name", :name%>

, remove :name and have

<%=label "name"%>

This will stop the wrapping.

Shiva
  • 11,485
  • 2
  • 67
  • 84
Antonio Jha
  • 1,301
  • 13
  • 13
  • 1
    This removes the connection between the ` – user2985898 Mar 02 '17 at 09:56