-
Notifications
You must be signed in to change notification settings - Fork 1
/
willvalidate.js
42 lines (30 loc) · 1.25 KB
/
willvalidate.js
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
import Vue from 'vue'
const clearErrors = form => {
Array.from( form.querySelectorAll('.has-danger') ).map( el => el.classList.remove('has-danger') )
Array.from( form.querySelectorAll('small') ).map( el => el.remove() )
}
const willvalidate = {
install(Vue, options) {
Vue.prototype.willvalidate = form => {
clearErrors(form)
let firstError=null
Array.from( form ).map( field => {
if (field.willValidate && !field.checkValidity())
{
if (!firstError) firstError = field
const small = document.createElement('small')
if(field.getAttribute("errorMessage"))
small.innerHTML = field.getAttribute("errorMessage")
else
small.innerHTML = field.validationMessage
small.className = 'form-control-feedback'
field.parentElement.appendChild(small)
field.parentElement.classList.add('has-danger')
}
})
if (firstError) firstError.focus()
return firstError === null
}
}
}
export default willvalidate