When validating the form, using chosen-select does not validate the selects, but removing ochosen-select already works. I show the example:
JavaScript
x
21
21
1
$("#validate").on("click",function(){
2
if(!$(".alerta").valid());
3
});
4
5
$(".alerta").validate({
6
messages: {
7
Tipo: "Tipo de alerta obrigatório",
8
Prioridade: {
9
required: "Prioridade obrigatório"
10
}
11
}
12
});
13
14
15
$(".chosen-select").chosen({
16
disable_search_threshold: 5,
17
no_results_text: "Sem resultados",
18
placeholder_text_multiple: "Selecione opções",
19
width: "100%"
20
});
21
JavaScript
1
27
27
1
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/chosen/1.8.7/chosen.min.css">
2
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
3
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery-validate/1.19.3/jquery.validate.min.js"></script>
4
<script src="https://cdnjs.cloudflare.com/ajax/libs/chosen/1.8.7/chosen.jquery.min.js"></script>
5
6
<form method="POST" class="row g-3 alerta">
7
<div class="col-md-4">
8
<label for="Tipo" class="form-label">TIPO DE ALERTA <span style="color: red;">*</span></label>
9
<select class="form-select chosen-select" name="Tipo" id="Tipo" required>
10
<option></option>
11
<option value="1">Teste</option>
12
<option value="2">Teste1</option>
13
</select>
14
</div>
15
16
<div class="col-md-4">
17
<label for="Tipo" class="form-label">Prioridade <span style="color: red;">*</span></label>
18
<select class="form-select" name="Prioridade" id="Prioridade" required>
19
<option></option>
20
<option value="1">Prioridade</option>
21
<option value="2">Prioridade1</option>
22
</select>
23
</div>
24
<div class="col-12">
25
<button type="button" class="btn-wide btn btn-success" style="float: right; margin-right: 5%; margin-top: 1%;" id="validate"> Enviar <i class="metismenu-icon pe-7s-paper-plane"></i></button>
26
</div>
27
</form>
In the select in which I use chosen-select, there is no value selected, but it does not show the message to the user as the field is mandatory.
Advertisement
Answer
Chosen-select adds visibility:hidden
attribute in select box.
You have to validate chosen-select forcefully.
JavaScript
1
2
1
$.validator.setDefaults({ ignore: ":hidden:not(.chosen-select)" })
2
Please add above line just before (".alerta").validate()
method.