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

Commit 434213a

Browse files
committed
📝 docs(gitbook): add results section
1 parent 080eb9d commit 434213a

File tree

1 file changed

+189
-0
lines changed

1 file changed

+189
-0
lines changed

gitbook/en/results.md

Lines changed: 189 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,189 @@
1+
# Validation Results
2+
3+
## Structure
4+
5+
### `validity` Or `validity-group` Component
6+
7+
When validate the target element with `validity` or `validity-group` component, validation results can get the `result` property from `validity` object. That validation results will be structured the below.
8+
9+
- `valid`: whether target element is valid; if it's valid, then return `true`, else return `false`.
10+
- `invalid`: reverse of `valid`.
11+
- `touched`: whether target element is touched. if target element was focusout or `touch` method, return `true`, else return `false`.
12+
- `untouched`: reverse of `touched`.
13+
- `modified`: whether target element value is modified; if target element value was changed from **initial** value, return `true`, else return `false`.
14+
- `dirty`: whether target element value was changed at least **once**; if so, return `true`, else return `false`.
15+
- `pristine`: reverse of `dirty`.
16+
- `errors`: if invalid target element exist, return error object wrapped with array; error object include field name, validator name, and error message,
17+
wrapped with array, else `undefined`.
18+
19+
In addtion other than those above, some validators which you specified to `validators` property with `validity` component keep as validation results. that validation result value, if it's valid, then return `false`, else (invalid) return `true`.
20+
21+
the blow is validation result example via `result` property of `validity` object:
22+
23+
```json
24+
{
25+
"valid": false,
26+
"invalid": true,
27+
"dirty": true,
28+
"pristine": false,
29+
"touched": true,
30+
"untouched": false,
31+
"modified": false,
32+
"errors": [ // validation errors
33+
{
34+
"field": "username",
35+
"validator": "minlength",
36+
"message": "too short username!!"
37+
}
38+
],
39+
"required": false, // build-in validator, return `false` or `true`
40+
"minlength": true, // build-in validator
41+
"email": false // custom validator, return `false or `true
42+
}
43+
```
44+
45+
### `validation` Component
46+
47+
When use the `validation` component and `group` property of `validity` or `validity-group` component, validation resutls can get the `$validation` property of Vue instance. That validation results will be structured the below.
48+
49+
- `valid`: whether **all** fields is valid. if so, then return `true`, else return `false`.
50+
- `invalid`: if invalid field exist even **one** in validate fields, return `true`, else `false`.
51+
- `touched`: if touched field exist **one** in validate fields, return `true`, else `false`.
52+
- `untouched`: whether **all** fields is untouched, if so, return `true`, else `false`.
53+
- `modified`: if modified field exist even **one** in validate fields, return `true`, else `false`.
54+
- `dirty`: if dirty field exist even **one** in validate fields, return `true`, else `false`.
55+
- `pristine`: whether **all** fields is pristine, if so, return `true`, else `false`.
56+
- `errors`: if invalid even one exist, return all field error message wrapped with array, else `undefined`.
57+
58+
The blow is validation result example via `$validation` property of Vue instance:
59+
60+
```json
61+
{
62+
"validation1": { // key is `name` property of `validation` component
63+
"valid": false, // valid of all fields
64+
"invalid": true, // invalid of all fields
65+
"dirty": true, // dirty of all fields
66+
"pristine": false, // pristine of all fields
67+
"touched": true, // touched of all fields
68+
"untouched": false, // untouched of all fields
69+
"modified": true, // modified of all fields
70+
"errors": [ // errors of all fields
71+
{
72+
"field": "username",
73+
"validator": "required"
74+
},
75+
{
76+
"field": "confirm",
77+
"validator": "minlength"
78+
}
79+
],
80+
"user": { // 'user' group
81+
"valid": false, // valid of 'user' group
82+
"invalid": true, // invalid of 'user' group
83+
"dirty": true, // dirty of 'user' group
84+
"pristine": false, // pristine of 'user' group
85+
"touched": true, // touched of 'user' group
86+
"untouched": false, // untouched of 'user' group
87+
"modified": false, // modified of 'user' group
88+
"errors": [ // errors of 'user' group
89+
{
90+
"field": "username",
91+
"validator": "required"
92+
}
93+
],
94+
"username": { // 'username' field: validity object
95+
"valid": false,
96+
"invalid": true,
97+
"dirty": true,
98+
"pristine": false,
99+
"touched": true,
100+
"untouched": false,
101+
"modified": false,
102+
"errors": [
103+
{
104+
"field": "username",
105+
"validator": "required"
106+
}
107+
],
108+
"required": true
109+
}
110+
},
111+
"password": { // 'password' group
112+
"valid": false, // valid of 'password' group
113+
"invalid": true, // invalid of 'password' group
114+
"dirty": true, // dirty of 'password' group
115+
"pristine": false, // pristine of 'password' group
116+
"touched": true, // touched of 'password' group
117+
"untouched": false, // untouched of 'password' group
118+
"modified": true, // modified of 'password' group
119+
"errors": [ // errors of 'password' group
120+
{
121+
"field": "confirm",
122+
"validator": "minlength"
123+
}
124+
],
125+
"password": { // 'password' field: validity object
126+
"valid": true,
127+
"invalid": false,
128+
"dirty": true,
129+
"pristine": false,
130+
"touched": true,
131+
"untouched": false,
132+
"modified": true,
133+
"minlength": false,
134+
"required": false
135+
},
136+
"confirm": { // 'confirm' field: validity object
137+
"valid": false,
138+
"invalid": true,
139+
"dirty": true,
140+
"pristine": false,
141+
"touched": true,
142+
"untouched": false,
143+
"modified": true,
144+
"errors": [
145+
{
146+
"field": "confirm",
147+
"validator": "minlength"
148+
}
149+
],
150+
"minlength": true,
151+
"required": false
152+
}
153+
}
154+
}
155+
}
156+
```
157+
158+
## Sync with `v-model`
159+
160+
The validation results can get the `result` property from `validity` object. However, synchronizing the validation results with the `data` of Vue instance is bit hassle. You can resolve with using `v-model`.
161+
162+
```html
163+
<div id="app">
164+
<label for="username">username:</label>
165+
<validity field="username" v-model="validation" :validators="{ required: true, minlength: 4 }">
166+
<input type="text" @input="handleValidate">
167+
</validity>
168+
<div class="errors">
169+
<p class="required" v-if="validation.result.required">required username!!</p>
170+
<p class="minlength" v-if="validation.result.minlength">too short username!!</p>
171+
</div>
172+
</div>
173+
```
174+
```javascript
175+
new Vue({
176+
data: {
177+
validation: {
178+
result: {} // initial validation result
179+
}
180+
},
181+
methods: {
182+
handleValidate: function (e) {
183+
e.target.$validity.validate()
184+
}
185+
}
186+
}).$mount('#app')
187+
```
188+
189+
As the above example, the validation results of `validity` object can be synced `validation` in `data` with `v-model` directive.

0 commit comments

Comments
 (0)