@@ -3,15 +3,14 @@ Swift-Validator
33
44Swift Validator is a rule-based validation library for Swift.
55
6+
67![ Swift Validator] ( /swift-validator-v2.gif )
78
89## Core Concepts
910
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.
1514
1615## Quick Start
1716
@@ -23,38 +22,43 @@ Initialize the ``Validator`` by setting a delegate to a View Controller or other
2322
2423let validator = Validator ()
2524
26- override func viewDidLoad () {
27- super .viewDidLoad ()
28- }
29-
3025```
3126
3227Register the fields that you want to validate
3328
3429``` 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}" )])
3545
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+ ```
4148
42- // You can validate against other fields using ConfirmRule
43- validator.registerField (emailConfirmTextField, errorLabel : emailConfirmErrorLabel, rules : [ConfirmationRule (confirmField : emailTextField)])
4449
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.
4851
52+ ``` swift
53+ @IBAction func signupTapped (sender : AnyObject ) {
54+ validator.validateAll (delegate :self )
55+ }
4956```
5057
51-
52- Validate All Fields
58+ Implement the Validation Delegate in your View controller
5359
5460``` swift
5561
56- validator.validateAll (delegate :self )
57-
5862// ValidationDelegate methods
5963
6064func validationWasSuccessful () {
@@ -63,47 +67,53 @@ func validationWasSuccessful() {
6367
6468func validationFailed (errors :[UITextField:ValidationError]) {
6569 // 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+ }
7276}
7377
7478```
7579
7680## Custom Validation
7781
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.
7983
80- Create a class that implements the Validation protocol
84+ Create a class that implements the Rule protocol
8185
8286``` swift
8387
8488class 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+ }
8697
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 ) {
89100 if ssnTest.evaluateWithObject (value) {
90- return ( true , . NoError )
101+ return true
91102 }
92- return ( false , . SocialSecurity ) // We will create this later ValidationErrorType.SocialSecurity
103+ return false
93104 }
94- return (false , .SocialSecurity )
95105 }
96-
106+
97107 func errorMessage () -> String {
98108 return " Not a valid SSN"
99109 }
100-
101110}
111+ ```
102112
103113Credits
104114-------
105115
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 ) .
107117
108118## Contributing
109119
0 commit comments