# Full Example
The following is a full example with the more widely used components
# Example form
Last Result:
null
# Example code
The example above consists of 3 files that contain a custom validator, validations file, and a vue component to house the form. The hope is that this example will cover most use cases for how to use the VRx Form package well.
# Custom Validator
This file contains the custom validator that will asynchronously validate a component validators/username-exists.js
:
import { VRXFormCustomValidator } from 'vrx-form';
export default class UsernameExistsValidator extends VRXFormCustomValidator {
constructor(message, options) {
super('usernameExists', message || 'This username already exists', options);
}
inList(value) {
return ['bestuser', 'hello_user', 'foo_user'].includes(value);
}
async validate(value) {
// proving that asynchronous methods can be handled in the platform
return !this.inList(value);
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
# Validations file
This file contains all of the validations of the field validations/profile-validations.js
:
import { VRXFormValidatorTypes } from 'vrx-form';
import UsernameExistsValidator from '../validators/username-exists';
export default {
username: [
{
type: VRXFormValidatorTypes.PATTERN,
validation: /^([a-zA-Z0-9_]+)$/,
message: 'Username can only contain numbers, letters and underscores.'
},
{
type: UsernameExistsValidator
},
{
type: VRXFormValidatorTypes.MIN_LENGTH,
validation: 3,
message: 'Username must be at least 3 characters long'
}
],
password: [
{
type: VRXFormValidatorTypes.PATTERN,
validation: /^(?=.*[A-Za-z])(?=.*\d)([A-Za-z\d@$!%*#?&]+)$/,
message: 'Password must contain at least 1 letter and 1 number'
},
{
type: VRXFormValidatorTypes.RANGE_LENGTH,
validation: [8,16],
message: 'Must be between 8 and 16 characters long'
}
],
fullName: [
{
type: VRXFormValidatorTypes.REQUIRED,
validation: false
},
{
type: VRXFormValidatorTypes.RANGE_LENGTH,
validation: [3,26]
}
],
bio: [
{
type: VRXFormValidatorTypes.RANGE_LENGTH,
validation: [10,100],
message: 'The bio should be between 10 and 100 characters long.'
}
],
agree: [
{
type: VRXFormValidatorTypes.REQUIRED,
validation: true,
message: 'You must check the box to proceed'
}
],
hear: [
{
type: VRXFormValidatorTypes.REQUIRED,
validation: true,
message: 'Please select how you heard about us.'
}
],
receiveEmail: [
{
type: VRXFormValidatorTypes.REQUIRED,
validation: true
}
]
}
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
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
# Vue component file
This file brings everything together to show how the vue validator works components/FullExample.vue
:
<template>
<div>
<form v-form="profileForm">
<div>
Form attributes:
<ul>
<li>isDirty: {{profileForm.isDirty}}</li>
<li>isPristine: {{profileForm.isPristine}}</li>
<li>isValid: {{profileForm.isValid}}</li>
</ul>
</div>
<div class="input-block">
<label for="profile.username">Username*</label>
<input
name="profile.username"
type="text"
v-model="profile.username"
v-form-field >
<p class="hint">
<span class="errors" v-if="usernameError">{{usernameError}}</span>
<span v-else>Hint: At least 3 characters long</span>
</p>
</div>
<div class="input-block">
<label for="profile.password">Password*</label>
<input
active-error
name="profile.password"
type="password"
v-model="profile.password"
v-form-field >
<p class="hint">
<span class="errors" v-if="passwordError">{{passwordError}}</span>
<span v-else>Hint: Must contain letters and numbers</span>
</p>
</div>
<div class="input-block">
<label for="profile.fullName">Full name</label>
<input
name="profile.fullName"
type="text"
v-model="profile.fullName"
v-form-field >
<p class="hint">
<span class="errors" v-if="fullNameError">{{fullNameError}}</span>
<span v-else>Hint: Optional</span>
</p>
</div>
<div class="input-block">
<label for="profile.bio">Bio*</label>
<textarea
name="profile.bio"
v-model="profile.bio"
rows="4"
v-form-field ></textarea>
<p class="hint">
<span class="errors" v-if="bioError">{{bioError}}</span>
<span v-else>Hint: Must be at least 10 characters long.</span>
</p>
</div>
<div class="input-block">
<label for="profile.agree">
<input
type="checkbox"
name="profile.agree"
v-model="profile.agree"
v-form-field
active-error
/>
Do you agree to the terms and conditions?</label>
<p class="hint">
<span class="errors" v-if="agreeError">{{agreeError}}</span>
<span v-else>Hint: Must check the box to continue.</span>
</p>
</div>
<div class="input-block">
<label for="profile.hear">
<select
name="profile.hear"
v-model="profile.hear"
v-form-field
active-error
>
<option></option>
<option value="google">Google</option>
<option value="yahoo">Yahoo</option>
<option value="google">Friend</option>
<option value="other">Other</option>
</select>
How did you hear about us?</label>
<p class="hint">
<span class="errors" v-if="hearError">{{hearError}}</span>
<span v-else>Hint: Must select one.</span>
</p>
</div>
<div class="input-block">
<label for="profile.receiveEmail">Would you like to receive emails?</label>
<input
type="radio"
name="profile.receiveEmail"
v-model="profile.receiveEmail"
v-form-field
active-error
value="true"
/> <span>Yes</span>
<input
type="radio"
name="profile.receiveEmail"
v-model="profile.receiveEmail"
v-form-field
active-error
value="false"
/> <span>No</span>
<p class="hint">
<span class="errors" v-if="receiveEmailError">{{receiveEmailError}}</span>
<span v-else>Hint: Must check the box to continue.</span>
</p>
</div>
<button @click="submit" :disabled="!profileForm.isValid">Submit</button>
</form>
<div>
<h3>Last Result:</h3>
<pre class="language-json">{{ profileValue | prettyJSON }}</pre>
</div>
</div>
</template>
<script>
import profileValidations from '../validations/profile-validations';
export default {
data() {
return {
profileForm: this.$createForm(),
profile: {
username: null,
password: null,
fullName: null,
bio: null,
agree: null,
hear: null,
receiveEmail: null
},
profileValue: null
}
},
mounted() {
this.profileValue = { ...this.profile };
this.profileForm
.setValidations(profileValidations)
.init();
},
methods: {
submit(){
this.profileValue = { ...this.profile };
this.profileForm.clearForm();
}
},
computed: {
usernameError() {
return this.profileForm.getError("username");
},
passwordError() {
return this.profileForm.getError("password");
},
bioError() {
return this.profileForm.getError("bio");
},
fullNameError() {
return this.profileForm.getError("fullName");
},
agreeError() {
return this.profileForm.getError("agree");
},
hearError() {
return this.profileForm.getError("hear");
},
receiveEmailError() {
return this.profileForm.getError("receiveEmail");
}
},
filters: {
prettyJSON: function(value) {
return JSON.stringify(value, null, 2);
}
}
}
</script>
<style scoped>
input[invalid], textarea[invalid], select[invalid] {
border: 1px solid #e74c3c !important;
}
.errors {
color: #e74c3c;
}
.result{
color: white;
}
p.hint {
margin: 0;
font-size: 0.75rem;
font-style: italic;
height: 1rem;
}
.input-block {
position: relative;
box-sizing: border-box;
min-height: 6rem;
margin-bottom: 0.5rem;
}
input:not([type="radio"]), textarea, select {
border-radius: 0.25rem;
padding: 0.25rem 0.5rem;
min-height: 2rem;
margin-bottom: 0.5rem;
font-size: 1rem;
border:1px solid #cccccc;
width: 50%;
}
input[name="profile.agree"] {
margin-top: 2rem !important;
width: inherit;
}
label:not([for="profile.agree"]) {
display: block;
margin-bottom: 0.5rem;
}
button {
display: inline-block;
border-radius: 0.25rem;
border: none;
box-sizing: border-box;
height: 2rem;
padding: 0.25rem 2rem;
margin: 0.5rem 0;
text-decoration: none;
background: #27ae60;
color: #ffffff;
font-family: sans-serif;
font-size: 1rem;
cursor: pointer;
text-align: center;
transition: background 250ms ease-in-out,
transform 150ms ease;
-webkit-appearance: none;
-moz-appearance: none;
}
button:hover,
button:focus, :not(button:disabled) {
background: #0053ba;
}
button:focus, :not(button:disabled) {
outline: 1px solid #fff;
outline-offset: -0.25rem;
}
button:active, :not(button:disabled) {
transform: scale(0.99);
}
button:disabled {
opacity: .5;
cursor: not-allowed;
}
</style>
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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268