Skip to content

Instantly share code, notes, and snippets.

@pietromoro
Last active May 2, 2022 16:48
Show Gist options
  • Save pietromoro/baf4849eb50c23c817f9f7301e685e1a to your computer and use it in GitHub Desktop.
Save pietromoro/baf4849eb50c23c817f9f7301e685e1a to your computer and use it in GitHub Desktop.
Rails bad input restyle with bootstrap initializer file
# Be sure to restart your server when you modify this file.
# Restyle how errors are shown
ActionView::Base.field_error_proc = proc do |html_tag, instance|
html = %(<div class="field-with-errors">#{html_tag}</div>).html_safe
form_fields = %w[textarea input select]
elements = Nokogiri::HTML::DocumentFragment.parse(html_tag).css 'label, ' + form_fields.join(', ')
elements.each do |element|
next if element.attributes.include? "data-disable-error-styling"
if element.node_name.eql? 'label'
html = %(<div class='control-group error'>#{element}</div>)
elsif form_fields.include? element.node_name
html = %(<div class='control-group error'>#{element.add_class 'is-invalid'}<div class='invalid-feedback'>&nbsp;#{instance.error_message.to_a.uniq.join(', ')}</div></div>)
end
end
html.html_safe
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment