Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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

This comment has been minimized.

Copy link

@thedanielhanke thedanielhanke commented Dec 17, 2014

L8: add .html_safe for newer rails versions

@agungyuliaji

This comment has been minimized.

Copy link

@agungyuliaji agungyuliaji commented Aug 2, 2018

thanks!

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