Styling form error message - bootstrap/rails

前端 未结 8 611
陌清茗
陌清茗 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:29

    Bootstrap 4 Alpha 6

    I copied the compiled Bootstrap CSS from

    https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-alpha.6/css/bootstrap.css

    Searched for .has-danger, copied all the classes, did a search & replace on .has-danger for .field_with_errors, and I also added .field_with_errors label

    .field_with_errors label,
    .field_with_errors .form-control-feedback,
    .field_with_errors .form-control-label,
    .field_with_errors .col-form-label,
    .field_with_errors .form-check-label,
    .field_with_errors .custom-control {
      color: #d9534f;
    }
    
    .field_with_errors .form-control {
      border-color: #d9534f;
    }
    
    .field_with_errors .input-group-addon {
      color: #d9534f;
      border-color: #d9534f;
      background-color: #fdf7f7;
    }
    
    .field_with_errors .form-control-danger {
      background-image: url("data:image/svg+xml;charset=utf8,%3Csvg xmlns='http://www.w3.org/2000/svg' fill='%23d9534f' viewBox='-2 -2 7 7'%3E%3Cpath stroke='%23d9534f' d='M0 0l3 3m0-3L0 3'/%3E%3Ccircle r='.5'/%3E%3Ccircle cx='3' r='.5'/%3E%3Ccircle cy='3' r='.5'/%3E%3Ccircle cx='3' cy='3' r='.5'/%3E%3C/svg%3E");
    }
    

    I wasn't able to get the input groups addons to display correctly, as it wraps the input with a <div>.

    Docs: https://v4-alpha.getbootstrap.com/components/forms/#validation

    Honestly some of these classes are not used because Rails doesn't have an obvious way to set classes on error fields.

    For the error list, I just used this simple class

    #error_explanation {
      color: red;
    }
    
    0 讨论(0)
  • 2021-02-01 19:29

    Maybe a simpler one is search for ids and classes on the form itself. Works for any combo.

    By default, this are the lines included in scaffold to arrange the error messages. You can do with them whatever you want. Just have to extend them in your css.scss file:

    .field_with_errors {
      padding: 2px;
      background-color: red;
      display: table;
    }
    
    #error_explanation {
      width: 450px;
      border: 2px solid red;
      padding: 7px 7px 0;
      margin-bottom: 20px;
      background-color: #f0f0f0;
    }
    
    #error_explanation h2 {
      text-align: left;
      font-weight: bold;
      padding: 5px 5px 5px 15px;
      font-size: 12px;
      margin: -7px -7px 0;
      background-color: #c00;
      color: #fff;
    }
    
    #error_explanation ul li {
      font-size: 12px;
      list-style: square;
    }
    

    In case something is not working, check the navigator in developer mode. There you should be able to find all the html and css rails is creating...

    0 讨论(0)
提交回复
热议问题