@@ -16,38 +16,32 @@ import UIKit
1616
1717public class Validator {
1818 // dictionary to handle complex view hierarchies like dynamic tableview cells
19- public var errors : [ UITextField : ValidationError ] = [ : ]
20- public var validations : [ UITextField : ValidationRule ] = [ : ]
19+ public var errors = [ UITextField : ValidationError ] ( )
20+ public var validations = [ UITextField : ValidationRule ] ( )
2121 private var successStyleTransform : ( ( validationRule: ValidationRule ) -> Void ) ?
2222 private var errorStyleTransform : ( ( validationError: ValidationError ) -> Void ) ?
2323
2424 public init ( ) { }
2525
2626 // MARK: Private functions
2727
28- private func clearErrors( ) {
29- self . errors = [ : ]
30- }
31-
3228 private func validateAllFields( ) {
3329
34- self . clearErrors ( )
30+ errors = [ : ]
3531
36- for field in validations. keys {
37- if let currentRule: ValidationRule = validations [ field] {
38- if var error: ValidationError = currentRule. validateField ( ) {
39- errors [ field] = error
40-
41- // let the user transform the field if they want
42- if let transform = self . errorStyleTransform {
43- transform ( validationError: error)
44- }
45- } else {
46- // No error
47- // let the user transform the field if they want
48- if let transform = self . successStyleTransform {
49- transform ( validationRule: currentRule)
50- }
32+ for (textField, rule) in validations {
33+ if var error = rule. validateField ( ) {
34+ errors [ textField] = error
35+
36+ // let the user transform the field if they want
37+ if let transform = self . errorStyleTransform {
38+ transform ( validationError: error)
39+ }
40+ } else {
41+ // No error
42+ // let the user transform the field if they want
43+ if let transform = self . successStyleTransform {
44+ transform ( validationRule: rule)
5145 }
5246 }
5347 }
0 commit comments