Skip to content

Instantly share code, notes, and snippets.

@virolea
Created January 21, 2022 10:16
Show Gist options
  • Save virolea/ab53ef661eb8997834069254a4259bf4 to your computer and use it in GitHub Desktop.
Save virolea/ab53ef661eb8997834069254a4259bf4 to your computer and use it in GitHub Desktop.
Custom input field error for Rails forms
ActionView::Base.field_error_proc = proc do |html_tag, instance_tag|
fragment = Nokogiri::HTML.fragment(html_tag)
field = fragment.at("input,select,textarea")
html = if field
field["class"] = "#{field["class"]} is-invalid"
html = <<-HTML
#{fragment}
<p class="invalid-feedback">#{instance_tag.error_message.to_sentence}</p>
HTML
html
else
html_tag
end
html.html_safe
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment