forked from form-validation/examples
-
Notifications
You must be signed in to change notification settings - Fork 0
/
select2.html
126 lines (115 loc) · 5.62 KB
/
select2.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
<!DOCTYPE html>
<html lang="en">
<head>
<title>FormValidation example - Integrate with select2</title>
<meta charset="utf-8" />
<meta content="width=device-width,initial-scale=1" name="viewport" />
<link
rel="stylesheet"
href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css"
/>
<link
rel="stylesheet"
href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.1.2/css/bootstrap.min.css"
/>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/select2/4.0.7/css/select2.min.css" />
<!-- Replace with your path -->
<link rel="stylesheet" href="/vendors/@form-validation/umd/styles/index.min.css" />
</head>
<body>
<form id="demoForm" method="POST">
<div class="form-group row">
<label class="col-md-3 col-form-label">Favorite color</label>
<div class="col-md-5">
<select name="colors" class="form-control" multiple data-placeholder="Choose 2-4 colors">
<option value="black">Black</option>
<option value="blue">Blue</option>
<option value="green">Green</option>
<option value="orange">Orange</option>
<option value="red">Red</option>
<option value="yellow">Yellow</option>
<option value="white">White</option>
</select>
</div>
</div>
<div class="form-group row">
<label class="col-md-3 col-form-label">Favorite color using tags</label>
<div class="col-md-5">
<select
class="form-control"
name="colors-tags"
multiple
data-placeholder="Choose 2-4 colors"
></select>
</div>
</div>
<div class="form-group row">
<div class="col-md-9 offset-md-3">
<button type="submit" class="btn btn-primary">Submit</button>
</div>
</div>
</form>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/select2/4.0.7/js/select2.min.js"></script>
<!-- Replace with your path -->
<script src="/vendors/@form-validation/umd/bundle/popular.min.js"></script>
<script src="/vendors/@form-validation/umd/plugin-bootstrap/index.min.js"></script>
<script>
document.addEventListener('DOMContentLoaded', function (e) {
const demoForm = document.getElementById('demoForm');
const $colorField = $(demoForm.querySelector('[name="colors"]'));
const $colorTagField = $(demoForm.querySelector('[name="colors-tags"]'));
const fv = FormValidation.formValidation(demoForm, {
fields: {
colors: {
validators: {
callback: {
message: 'Please choose 2-4 color you like most',
callback: function (input) {
// Get the selected options
const options = $colorField.select2('data');
return options != null && options.length >= 2 && options.length <= 4;
},
},
},
},
'colors-tags': {
validators: {
callback: {
message: 'Please choose 2-4 color you like most',
callback: function (input) {
// Get the selected options
const options = $colorTagField.select2('data');
return options !== null && options.length >= 2 && options.length <= 4;
},
},
},
},
},
plugins: {
trigger: new FormValidation.plugins.Trigger(),
submitButton: new FormValidation.plugins.SubmitButton(),
bootstrap: new FormValidation.plugins.Bootstrap(),
icon: new FormValidation.plugins.Icon({
valid: 'fa fa-check',
invalid: 'fa fa-times',
validating: 'fa fa-refresh',
}),
},
});
$colorField.select2().on('change.select2', function () {
// Revalidate the `color` field when an option is chosen
fv.revalidateField('colors');
});
$colorTagField
.select2({
tags: ['Black', 'Blue', 'Green', 'Orange', 'Red', 'Yellow', 'White'],
})
.on('change.select2', function () {
// Revalidate the `color-tags` field when a tag is selected
fv.revalidateField('colors-tags');
});
});
</script>
</body>
</html>