1
1
# Form validation module
2
2
3
- ![ logo] ( https://github.com/rsamec/form/blob/master/form_logo.gif )
3
+ ![ logo] ( https://github.com/rsamec/form/blob/master/form_logo.jpg )
4
4
5
- Validation module is a lightweight JavaScript library for easy business rules definition of the product, the contract or validation rules of the form.
5
+ Validation module is a lightweight JavaScript library for easy business rules definition of the product, the contract, the form etc.
6
+
7
+ [ API + tutorial] (https://github.com/rsamec/form/wiki )
6
8
7
9
## Installation
8
10
@@ -16,6 +18,7 @@ This module is installed via bower:
16
18
17
19
Add reference to dist/form.min.js file.
18
20
21
+ To create abstract validator
19
22
``` js
20
23
// create new validator for object with structure<IPerson>
21
24
var personValidator = new Validation.AbstractValidator ();
@@ -32,12 +35,52 @@ Add reference to dist/form.min.js file.
32
35
personValidator .RuleFor (" LastName" , required);
33
36
personValidator .RuleFor (" LastName" ,maxLength);
34
37
35
- personValidator = personValidator .CreateRule (" Person" );
38
+ ```
39
+
40
+ To create test data
41
+ ``` js
42
+ // create test data
43
+ var data = {
44
+ Person1:
45
+ {
46
+ FirstName: ' John' ,
47
+ LastName: ' Smith'
48
+ },
49
+ Person2: {}
50
+
51
+ }
52
+
53
+ // create concrete rule
54
+ var person1Validator = personValidator .CreateRule (" Person 1" );
36
55
37
- var result = this .PersonValidator .Validate (this .Data );
56
+ // execute validation
57
+ var result = person1Validator .Validate (this .Data .Person1 );
58
+
59
+ // verify results
38
60
if (result .HasErrors ){
39
61
console .log (result .ErrorMessage );
40
62
}
63
+ // ---------
64
+ // --outputs
65
+ // ---------
66
+
67
+ // create concrete rule
68
+ var person2Validator = personValidator .CreateRule (" Person 2" );
69
+
70
+ // execute validation
71
+ var result = person2Validator .Validate (this .Data .Person1 );
72
+
73
+ // verify results
74
+ if (result .HasErrors ){
75
+ console .log (result .ErrorMessage );
76
+ }
77
+
78
+ // ---------
79
+ // --outputs
80
+ // ---------
81
+ // FirstName: Field is required.
82
+ // LastName: Field is required.
83
+
41
84
```
42
85
43
86
## Source code
0 commit comments