Parsley JS无法与引导选择一起使用

时间:2019-07-16 08:45:11

标签: bootstrap-4 parsley.js bootstrap-select

我使用https://parsleyjs.org/创建了一个表单,并与text input一起正常工作,并显示错误。但是当涉及到bootstrap-select时,它没有显示任何错误。 这是下面的一个小例子

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);
}

现在如何显示Bootstrap-select的错误

enter image description here

1 个答案:

答案 0 :(得分:1)

bootstrap-select插件会在更改时触发changed.bs.select事件。在triggerAfterFailure

中添加事件
$("form").parsley({
  ...
  triggerAfterFailure: 'focusout changed.bs.select'
})

无效引导选择的CSS

/* 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

未解决的问题