Skip to content

Instantly share code, notes, and snippets.

@stex
Created May 8, 2013 11:23
Show Gist options
  • Star 15 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save stex/5539836 to your computer and use it in GitHub Desktop.
Save stex/5539836 to your computer and use it in GitHub Desktop.
Rails field_error_proc to add an error class to invalid input fields
#Overrides the default way how rails marks form fields with errors.
#In this version, it only adds the class 'error' to label and element
ActionView::Base.field_error_proc = Proc.new do |html_tag, instance|
if html_tag =~ /<(input|label|textarea|select)/
html_field = Nokogiri::HTML::DocumentFragment.parse(html_tag)
html_field.children.add_class 'error'
html_field.to_s
else
html_tag
end
end
@thedanielhanke
Copy link

L8: add .html_safe for newer rails versions

@agungyuliaji
Copy link

thanks!

@mrshortt
Copy link

mrshortt commented Sep 6, 2023

@thedanielhanke is there any other way? Rubocop is complaining...

@thedanielhanke
Copy link

@mrshortt please provide some more details/infos

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment