@@ -3,15 +3,14 @@ Swift-Validator
3
3
4
4
Swift Validator is a rule-based validation library for Swift.
5
5
6
+
6
7
![ Swift Validator] ( /swift-validator-v2.gif )
7
8
8
9
## Core Concepts
9
10
10
- * `` UITextField `` + `` ValidationRule `` go into ```Validator``
11
- * `` ITextField `` + `` ValidationError `` come out of ```Validator``
12
- * `` UITextField `` is registered to `` Validator ``
13
- * `` Validator `` evaluates `` ValidationRules `` sequentially and stops evaluating when a `` ValidationRule `` fails.
14
- * Keys are used to allow field registration in TableViewControllers and complex view hierarchies
11
+ * `` UITextField `` + `` [Rule] `` + (and optional error `` UILabel `` ) go into `` Validator ``
12
+ * `` UITextField `` + `` ValidationError `` come out of ```Validator``
13
+ * `` Validator `` evaluates `` [Rule] `` sequentially and stops evaluating when a `` Rule `` fails.
15
14
16
15
## Quick Start
17
16
@@ -23,38 +22,43 @@ Initialize the ``Validator`` by setting a delegate to a View Controller or other
23
22
24
23
let validator = Validator ()
25
24
26
- override func viewDidLoad () {
27
- super .viewDidLoad ()
28
- }
29
-
30
25
```
31
26
32
27
Register the fields that you want to validate
33
28
34
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}" )])
35
45
36
- // Validation Rules are evaluated from left to right.
37
- validator.registerField (fullNameTextField, rules : [RequiredRule (), FullNameRule ()])
38
-
39
- // You can pass in error labels with your rules
40
- validator.registerField (emailTextField, errorLabel : emailErrorLabel, rules : [RequiredRule (), EmailRule ()])
46
+ }
47
+ ```
41
48
42
- // You can validate against other fields using ConfirmRule
43
- validator.registerField (emailConfirmTextField, errorLabel : emailConfirmErrorLabel, rules : [ConfirmationRule (confirmField : emailTextField)])
44
49
45
- // You can now pass in regex and length parameters through overloaded contructors
46
- validator.registerField (phoneNumberTextField, errorLabel : phoneNumberErrorLabel, rules : [RequiredRule (), MinLengthRule (length : 9 )])
47
- validator.registerField (zipcodeTextField, errorLabel : zipcodeErrorLabel, rules : [RequiredRule (), ZipCodeRule (regex = " \\ d{5}" )])
50
+ Validate Fields on button tap or however you would like to trigger it.
48
51
52
+ ``` swift
53
+ @IBAction func signupTapped (sender : AnyObject ) {
54
+ validator.validateAll (delegate :self )
55
+ }
49
56
```
50
57
51
-
52
- Validate All Fields
58
+ Implement the Validation Delegate in your View controller
53
59
54
60
``` swift
55
61
56
- validator.validateAll (delegate :self )
57
-
58
62
// ValidationDelegate methods
59
63
60
64
func validationWasSuccessful () {
@@ -63,47 +67,53 @@ func validationWasSuccessful() {
63
67
64
68
func validationFailed (errors :[UITextField:ValidationError]) {
65
69
// turn the fields to red
66
- for (field, error) in validator.errors {
67
- field.layer .borderColor = UIColor.redColor ().CGColor
68
- field.layer .borderWidth = 1.0
69
- error.errorLabel ? .text = error.errorMessage // works if you added labels
70
- error.errorLabel ? .hidden = false
71
- }
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
+ }
72
76
}
73
77
74
78
```
75
79
76
80
## Custom Validation
77
81
78
- We will create a ``` SSNValidation ``` class to show how to create your own Validation. A United States Social Security Number (or SSN) is a field that consists of XXX-XX-XXXX.
82
+ We will create a ``` SSNRule ``` class to show how to create your own Validation. A United States Social Security Number (or SSN) is a field that consists of XXX-XX-XXXX.
79
83
80
- Create a class that implements the Validation protocol
84
+ Create a class that implements the Rule protocol
81
85
82
86
``` swift
83
87
84
88
class SSNVRule : Rule {
85
- let SSN_REGEX = " ^\\ d{3}-\\ d{2}-\\ d{4}$"
89
+ let REGEX = " ^\\ d{3}-\\ d{2}-\\ d{4}$"
90
+
91
+ init (){}
92
+
93
+ // allow for custom variables to be passed
94
+ init (regex :String ){
95
+ self .REGEX = regex
96
+ }
86
97
87
- func validate (value : String ) -> ( Bool , ValidationErrorType) {
88
- if let ssnTest = NSPredicate (format : " SELF MATCHES %@" , SSN_REGEX ) {
98
+ func validate (value : String ) -> Bool {
99
+ if let ssnTest = NSPredicate (format : " SELF MATCHES %@" , REGEX ) {
89
100
if ssnTest.evaluateWithObject (value) {
90
- return ( true , . NoError )
101
+ return true
91
102
}
92
- return ( false , . SocialSecurity ) // We will create this later ValidationErrorType.SocialSecurity
103
+ return false
93
104
}
94
- return (false , .SocialSecurity )
95
105
}
96
-
106
+
97
107
func errorMessage () -> String {
98
108
return " Not a valid SSN"
99
109
}
100
-
101
110
}
111
+ ```
102
112
103
113
Credits
104
114
-------
105
115
106
- Swift Validator is written and maintained by Jeff Potter [@jpotts18 ](http :// twitter.com/jpotts18) and friends .
116
+ Swift Validator is written and maintained by Jeff Potter [ @jpotts18 ] ( http://twitter.com/jpotts18 ) .
107
117
108
118
## Contributing
109
119
0 commit comments