Created
January 21, 2022 10:16
-
-
Save virolea/ab53ef661eb8997834069254a4259bf4 to your computer and use it in GitHub Desktop.
Custom input field error for Rails forms
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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