@@ -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
@@ -49,12 +48,18 @@ validator.registerField(zipcodeTextField, errorLabel: zipcodeErrorLabel, rules:
4948```
5049
5150
52- Validate All Fields
51+ Validate Fields on button tap or as the fields
5352
5453``` swift
5554
5655validator.validateAll (delegate :self )
5756
57+ ```
58+
59+ Implement the Validation Delegate in your View controller
60+
61+ ``` swift
62+
5863// ValidationDelegate methods
5964
6065func validationWasSuccessful () {
@@ -75,36 +80,41 @@ func validationFailed(errors:[UITextField:ValidationError]) {
7580
7681## Custom Validation
7782
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.
83+ 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.
7984
80- Create a class that implements the Validation protocol
85+ Create a class that implements the Rule protocol
8186
8287``` swift
8388
8489class SSNVRule : Rule {
85- let SSN_REGEX = " ^\\ d{3}-\\ d{2}-\\ d{4}$"
90+ let REGEX = " ^\\ d{3}-\\ d{2}-\\ d{4}$"
8691
87- func validate (value : String ) -> (Bool , ValidationErrorType) {
88- if let ssnTest = NSPredicate (format : " SELF MATCHES %@" , SSN_REGEX) {
92+ init (){}
93+
94+ // allow for custom variables to be passed
95+ init (regex :String ){
96+ self .REGEX = regex
97+ }
98+
99+ func validate (value : String ) -> Bool {
100+ if let ssnTest = NSPredicate (format : " SELF MATCHES %@" , REGEX) {
89101 if ssnTest.evaluateWithObject (value) {
90- return ( true , . NoError )
102+ return true
91103 }
92- return ( false , . SocialSecurity ) // We will create this later ValidationErrorType.SocialSecurity
104+ return false
93105 }
94- return (false , .SocialSecurity )
95106 }
96-
107+
97108 func errorMessage () -> String {
98109 return " Not a valid SSN"
99110 }
100-
101111}
102112```
103113
104114Credits
105115-------
106116
107- Swift Validator is written and maintained by Jeff Potter [ @jpotts18 ] ( http://twitter.com/jpotts18 ) and friends .
117+ Swift Validator is written and maintained by Jeff Potter [ @jpotts18 ] ( http://twitter.com/jpotts18 ) .
108118
109119## Contributing
110120
0 commit comments