Styling form error message - bootstrap/rails

前端 未结 8 628
陌清茗
陌清茗 2021-02-01 18:38

The error messages for my rails form look terrible with bootstrap. Does anyone know a solution for better (nice looking) error messages? I use Rails and Bootstrap.

My fo

8条回答
  •  太阳男子
    2021-02-01 19:12

    I've create a custom initializer to have each field having its own errors below it

    # app/config/initializers/bootstrap_form_errors_customizer.rb
    
    ActionView::Base.field_error_proc = proc do |html_tag, instance|
      is_label_tag = html_tag =~ /^
" if error_msg.is_a?(Array) error_msg.each do |msg| html_list_errors.insert(-6,"
  • #{msg}
  • ") end else html_list_errors.insert(-6,"
  • #{msg}
  • ") end html_list_errors end invalid_div = "
    #{format_error_message_to_html_list(instance.error_message)}
    " if class_attr_index && !is_label_tag html_tag.insert(class_attr_index + 7, 'is-invalid ') html_tag + invalid_div.html_safe elsif !class_attr_index && !is_label_tag html_tag.insert(html_tag.index('>'), ' class="is-invalid"') html_tag + invalid_div.html_safe else html_tag.html_safe end end

    提交回复
    热议问题