@@ -16,38 +16,32 @@ import UIKit
16
16
17
17
public class Validator {
18
18
// 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 ] ( )
21
21
private var successStyleTransform : ( ( validationRule: ValidationRule ) -> Void ) ?
22
22
private var errorStyleTransform : ( ( validationError: ValidationError ) -> Void ) ?
23
23
24
24
public init ( ) { }
25
25
26
26
// MARK: Private functions
27
27
28
- private func clearErrors( ) {
29
- self . errors = [ : ]
30
- }
31
-
32
28
private func validateAllFields( ) {
33
29
34
- self . clearErrors ( )
30
+ errors = [ : ]
35
31
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)
51
45
}
52
46
}
53
47
}
0 commit comments