Search code examples
bootstrap-4parsley.jsbootstrap-select

Parsley JS not working with bootstrap-select


i created a form with https://parsleyjs.org/ and it works fine with text input and it displays error. But when it comes to bootstrap-select it doesn't show any error. Here is an small example below

https://jsfiddle.net/sanoj908572/jdo6reaf/2/

HTML

<div class="form-group">
     <label for="tit">Title</label>
     <input type="text" name="tit" class="form-control" id="tit" required>
</div>
<div class="form-group w-50">
     <label for="edu">Brand</label>
     <select name="brd" class="selectpicker form-control" id="edu" data-live-search="true" data-live-search-style="startsWith" data-parsley-required>
             <option data-display="Select Brand">Select Brand</option>
             <option>Audi</option> 
             <option>BMW</option> 
             <option>Chevrolet</option>
             <option>Fiat</option> 
             <option>Ford</option>
             <option>Hindustan Motors</option> 
     </select>
 </div>

JS

  $("#adform").parsley({
  errorClass: 'is-invalid text-danger',

  successClass: 'is-valid', // Comment this option if you don't want the field to become green when valid. Recommended in Google material design to prevent too many hints for user experience. Only report when a field is wrong.
  errorsWrapper: '<div class="input-group"></div>',
  errorTemplate: '<small class="form-text text-danger"></small>',
  trigger: 'change'
})

CSS

.was-validated .form-control:invalid, .form-control.is-invalid {
    border-color: #dc3545;
    padding-right: calc(1.5em + 0.75rem);
    background-image: url("data:image/svg+xml,%3csvg xmlns='http://www.w3.org/2000/svg' fill='%23dc3545' viewBox='-2 -2 7 7'%3e%3cpath stroke='%23dc3545' 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");
    background-repeat: no-repeat;
    background-position: center right calc(0.375em + 0.1875rem);
    background-size: calc(0.75em + 0.375rem) calc(0.75em + 0.375rem);
}

.was-validated .form-control:valid, .form-control.is-valid {
    border-color: #28a745;
    padding-right: calc(1.5em + 0.75rem);
    background-image: url("data:image/svg+xml,%3csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 8 8'%3e%3cpath fill='%2328a745' d='M2.3 6.73L.6 4.53c-.4-1.04.46-1.4 1.1-.8l1.1 1.4 3.4-3.8c.6-.63 1.6-.27 1.2.7l-4 4.6c-.43.5-.8.4-1.1.1z'/%3e%3c/svg%3e");
    background-repeat: no-repeat;
    background-position: center right calc(0.375em + 0.1875rem);
    background-size: calc(0.75em + 0.375rem) calc(0.75em + 0.375rem);
}

now how do i show error for Bootstrap-select

enter image description here


Solution

  • bootstrap-select plugin triggers the changed.bs.select event on change. Adding the event in triggerAfterFailure

    $("form").parsley({
      ...
      triggerAfterFailure: 'focusout changed.bs.select'
    })
    

    CSS for invalid bootstrap-select

    /* On Invalid */
    .selectpicker.is-invalid ~ .dropdown-toggle {
      border-color: #dc3545 !important;
      color: #dc3545 !important;
      outline: none !important;
    }
    
    .selectpicker.is-invalid ~ .dropdown-toggle:focus {
      box-shadow: 0 0 0 0.2rem rgba(220, 53, 69, 0.25) !important;
    }
    
    /* On valid */
    .selectpicker.is-valid ~ .dropdown-toggle {
      border-color: #28a745 !important;
      color: #28a745 !important;
      outline: none !important;
    }
    
    .selectpicker.is-valid ~ .dropdown-toggle:focus {
      box-shadow: 0 0 0 0.2rem rgba(40, 167, 69, 0.25) !important;
    }
    

    Fiddle

    Unclosed Issues