-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfromOptions.ts
More file actions
67 lines (58 loc) · 2.06 KB
/
fromOptions.ts
File metadata and controls
67 lines (58 loc) · 2.06 KB
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
import {
IErrorHandlerBaseOptions,
IErrorHandlerOptions,
IValidationInstanceOptions,
Validation,
} from '../src/validation';
import {
EmailValidator,
NumberRangeValidator,
NumberValidator,
RequiredValidator,
} from '../src/validation/validators';
const form = document.querySelector('form');
if (!form) {
throw new Error('No form');
}
// avoid submitting form for demo purposes
form.addEventListener('submit', e => {
e.preventDefault();
});
// prepare validation instance
// validation objects can be instantiated for multiple forms with differing
// options, becoming a "validation instance"
const validation = new Validation<
IValidationInstanceOptions,
// allow options for the selected error handler
IErrorHandlerBaseOptions & Partial<IErrorHandlerOptions>
>();
// configure validators
validation
.registerValidator(RequiredValidator)
.registerValidator(EmailValidator)
.registerValidator(NumberValidator)
.registerValidator(NumberRangeValidator);
// initialize validation instance for a certain form
const vi = validation.init(form, {
/* validationInstanceOptions: {
onSubmit(validationResult) {
console.info('form submitted. valid: %s', validationResult);
},
}, */
errorHandlerOptions: {
labels: {
errorListTitle: 'Please correct the following errors in the form:',
},
translations: {
validationMessages: {
required: label => `"${label}" is required`,
email: label => `"${label}" is not a valid email address`,
number: label => `"${label}" may only contain numbers`,
maxVal: (label, max) => `"${label}" has to be less than ${max}`,
minVal: (label, min) => `"${label}" has to be greater than ${min}`,
exactVal: (label, val) => `"${label}" has to be ${val}`,
rangeVal: (label, min, max) => `"${label}" has to be greater than ${min} and less than ${max}`,
},
},
},
});