@@ -22,38 +22,37 @@ Initialize the ``Validator`` by setting a delegate to a View Controller or other
22
22
23
23
let validator = Validator ()
24
24
25
- override func viewDidLoad () {
26
- super .viewDidLoad ()
27
- }
28
-
29
25
```
30
26
31
27
Register the fields that you want to validate
32
28
33
29
``` swift
30
+ override func viewDidLoad () {
31
+ super .viewDidLoad ()
32
+
33
+ // Validation Rules are evaluated from left to right.
34
+ validator.registerField (fullNameTextField, rules : [RequiredRule (), FullNameRule ()])
35
+
36
+ // You can pass in error labels with your rules
37
+ validator.registerField (emailTextField, errorLabel : emailErrorLabel, rules : [RequiredRule (), EmailRule ()])
38
+
39
+ // You can validate against other fields using ConfirmRule
40
+ validator.registerField (emailConfirmTextField, errorLabel : emailConfirmErrorLabel, rules : [ConfirmationRule (confirmField : emailTextField)])
41
+
42
+ // You can now pass in regex and length parameters through overloaded contructors
43
+ validator.registerField (phoneNumberTextField, errorLabel : phoneNumberErrorLabel, rules : [RequiredRule (), MinLengthRule (length : 9 )])
44
+ validator.registerField (zipcodeTextField, errorLabel : zipcodeErrorLabel, rules : [RequiredRule (), ZipCodeRule (regex = " \\ d{5}" )])
34
45
35
- // Validation Rules are evaluated from left to right.
36
- validator.registerField (fullNameTextField, rules : [RequiredRule (), FullNameRule ()])
37
-
38
- // You can pass in error labels with your rules
39
- validator.registerField (emailTextField, errorLabel : emailErrorLabel, rules : [RequiredRule (), EmailRule ()])
40
-
41
- // You can validate against other fields using ConfirmRule
42
- validator.registerField (emailConfirmTextField, errorLabel : emailConfirmErrorLabel, rules : [ConfirmationRule (confirmField : emailTextField)])
43
-
44
- // You can now pass in regex and length parameters through overloaded contructors
45
- validator.registerField (phoneNumberTextField, errorLabel : phoneNumberErrorLabel, rules : [RequiredRule (), MinLengthRule (length : 9 )])
46
- validator.registerField (zipcodeTextField, errorLabel : zipcodeErrorLabel, rules : [RequiredRule (), ZipCodeRule (regex = " \\ d{5}" )])
47
-
46
+ }
48
47
```
49
48
50
49
51
- Validate Fields on button tap or as the fields
50
+ Validate Fields on button tap or however you would like to trigger it.
52
51
53
52
``` swift
54
-
55
- validator.validateAll (delegate :self )
56
-
53
+ @IBAction func signupTapped ( sender : AnyObject ) {
54
+ validator.validateAll (delegate :self )
55
+ }
57
56
```
58
57
59
58
Implement the Validation Delegate in your View controller
@@ -68,12 +67,12 @@ func validationWasSuccessful() {
68
67
69
68
func validationFailed (errors :[UITextField:ValidationError]) {
70
69
// turn the fields to red
71
- for (field, error) in validator.errors {
72
- field.layer .borderColor = UIColor.redColor ().CGColor
73
- field.layer .borderWidth = 1.0
74
- error.errorLabel ? .text = error.errorMessage // works if you added labels
75
- error.errorLabel ? .hidden = false
76
- }
70
+ for (field, error) in validator.errors {
71
+ field.layer .borderColor = UIColor.redColor ().CGColor
72
+ field.layer .borderWidth = 1.0
73
+ error.errorLabel ? .text = error.errorMessage // works if you added labels
74
+ error.errorLabel ? .hidden = false
75
+ }
77
76
}
78
77
79
78
```
0 commit comments