Skip to content
This repository was archived by the owner on Dec 25, 2017. It is now read-only.

Commit bc31170

Browse files
committed
📝 docs(gitbook): add custom validator section
1 parent db4c45f commit bc31170

File tree

2 files changed

+211
-0
lines changed

2 files changed

+211
-0
lines changed

gitbook/en/SUMMARY.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,3 +14,4 @@
1414
- [Grouping](grouping.md)
1515
- [Error Messages](errors.md)
1616
- [Events](events.md)
17+
- [Custom Validator](custom.md)

gitbook/en/custom.md

Lines changed: 210 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,210 @@
1+
## Global registration
2+
You can register your custom validator with using `Vue.validator` method.
3+
4+
> **NOTE:** `Vue.validator` asset is extended from Vue.js' asset managment system.
5+
6+
Detail of the `Vue.validator` method.
7+
8+
The below the `email` custom validator exmpale:
9+
10+
```javascript
11+
// Register `email` validator function
12+
Vue.validator('email', {
13+
check: function (val) {
14+
return /^(([^<>()[\]\\.,;:\s@\"]+(\.[^<>()[\]\\.,;:\s@\"]+)*)|(\".+\"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/.test(val)
15+
}
16+
})
17+
18+
new Vue({
19+
data: {
20+
validation: {
21+
result: {}
22+
}
23+
}
24+
}).$mount('#app')
25+
```
26+
27+
```html
28+
<div id="app">
29+
<form novalidate>
30+
<label>Email:</label>
31+
<validity ref="validity" field="email" v-model="validation" :validators="['email']">
32+
<input type="text" @input="$refs.validity.validate()">
33+
</validity>
34+
<div class="errors">
35+
<p class="email" v-if="validation.result.email">Invalid your mail address format.</p>
36+
</div>
37+
<input type="submit" value="send" v-if="validation.result.valid">
38+
</form>
39+
</div>
40+
```
41+
42+
## Local registration
43+
You can register your custom validator to component with using `validators` option.
44+
45+
Custom validators are registered to Vue constructor `validators` option using a callback function; return true upon passing.
46+
47+
the below the `numeric` or `url` custom validator exmpale:
48+
49+
```javascript
50+
new Vue({
51+
validators: { // `numeric` and `url` custom validator is local registration
52+
numeric: function (val) {
53+
return /^[-+]?[0-9]+$/.test(val)
54+
},
55+
url: function (val) {
56+
return /^(http\:\/\/|https\:\/\/)(.{4,})$/.test(val)
57+
}
58+
},
59+
data: {
60+
results: {
61+
username: {},
62+
age: {},
63+
site: {}
64+
}
65+
},
66+
computed: {
67+
valid: function () {
68+
var results = this.results
69+
var fields = Object.keys(results)
70+
var ret = true
71+
for (var i = 0; i < fields.length; i++) {
72+
var field = fields[i]
73+
if (results[field].invalid) {
74+
ret = false
75+
break
76+
}
77+
}
78+
return ret
79+
}
80+
},
81+
methods: {
82+
handleUsername: function (e) {
83+
this.handleValidate(e.target.$validity, 'username')
84+
},
85+
handleAge: function (e) {
86+
this.handleValidate(e.target.$validity, 'age')
87+
},
88+
handleSite: function (e) {
89+
this.handleValidate(e.target.$validity, 'site')
90+
},
91+
handleValidate: function ($validity, field) {
92+
var self = this
93+
$validity.validate(function () {
94+
var result = $validity.result
95+
self.results[field] = result
96+
})
97+
}
98+
}
99+
}).$mount('#app')
100+
```
101+
102+
```html
103+
<div id="app">
104+
<form novalidate>
105+
<div class="username">
106+
<label>username:</label>
107+
<validity field="username" :validators="['required']">
108+
<input type="text" name="username" @input="handleUsername">
109+
</validity>
110+
</div>
111+
<div class="age">
112+
<label>age:</label>
113+
<validity field="age" :validators="['numeric']">
114+
<input type="text" name="age" @input="handleAge">
115+
</validity>
116+
</div>
117+
<div class="site">
118+
<label>site:</label>
119+
<validity field="site" :validators="['url']">
120+
<input type="text" name="site" @input="handleSite">
121+
</validity>
122+
</div>
123+
<div class="errors">
124+
<p class="username" v-if="results.username.required">required username!!</p>
125+
<p class="age" v-if="results.age.numeric">invalid age numeric value!!</p>
126+
<p class="site" v-if="results.site.url">invlid url!!</p>
127+
</div>
128+
<input type="submit" value="send" v-if="valid">
129+
</form>
130+
</div>
131+
```
132+
133+
## Error message
134+
135+
Custom validators may have default error messages attached:
136+
137+
```javascript
138+
// `email` custom validator is global registration
139+
Vue.validator('email', {
140+
check: function (val) {
141+
return /^(([^<>()[\]\\.,;:\s@\"]+(\.[^<>()[\]\\.,;:\s@\"]+)*)|(\".+\"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/.test(val)
142+
},
143+
message: 'Invalid your mail address format.' // error message with plain string
144+
})
145+
146+
// build-in `required` validator customization
147+
Vue.validator('required', {
148+
message: function (field) { // error message with function
149+
return 'required "' + field + '" field'
150+
},
151+
check: Vue.validator('required') // re-use validator logic
152+
})
153+
154+
new Vue({
155+
validators: {
156+
numeric: { // `numeric` custom validator local registration
157+
message: 'invalid numeric value',
158+
check: function (val) {
159+
return /^[-+]?[0-9]+$/.test(val)
160+
}
161+
},
162+
url: { // `url` custom validator local registration
163+
message: function (field) {
164+
return 'invalid "' + field + '" url format field'
165+
},
166+
check: function (val) {
167+
return /^(http\:\/\/|https\:\/\/)(.{4,})$/.test(val)
168+
}
169+
}
170+
},
171+
data: {
172+
validation: {
173+
result: {}
174+
}
175+
}
176+
}).$mount('#app')
177+
```
178+
179+
```html
180+
<div id="app">
181+
<form novalidate>
182+
<label>username:</label>
183+
<validity ref="validity" field="username" v-model="validation" :validators="['required']">
184+
<input type="text" @input="$refs.validity.validate()">
185+
</validity>
186+
<label>email:</label>
187+
<validity ref="validity" field="email" v-model="validation" :validators="['email']">
188+
<input type="text" @input="$refs.validity.validate()">
189+
</validity>
190+
<div class="age">
191+
<label>age:</label>
192+
<validity field="age" :validators="['numeric']">
193+
<input type="text" name="age" @input="handleAge">
194+
</validity>
195+
</div>
196+
<div class="site">
197+
<label>site:</label>
198+
<validity field="site" :validators="['url']">
199+
<input type="text" name="site" @input="handleSite">
200+
</validity>
201+
</div>
202+
<ul class="errors">
203+
<li v-for="error in validation">
204+
<p :class="error.validator">{{error.field}}: {{error.message}}</p>
205+
</li>
206+
</ul>
207+
<input type="submit" value="send" v-if="validation.result.valid">
208+
</form>
209+
</div>
210+
```

0 commit comments

Comments
 (0)