File tree Expand file tree Collapse file tree 2 files changed +29
-0
lines changed
Expand file tree Collapse file tree 2 files changed +29
-0
lines changed Original file line number Diff line number Diff line change @@ -57,6 +57,21 @@ public class Validator {
5757 }
5858 }
5959
60+ public func validate( callback: ( errors: [ UITextField : ValidationError ] ) -> Void ) -> Void {
61+
62+ for field in validations. keys {
63+ if let currentRule: ValidationRule = validations [ field] {
64+ if var error: ValidationError = currentRule. validateField ( ) {
65+ errors [ field] = error
66+ } else {
67+ errors. removeValueForKey ( field)
68+ }
69+ }
70+ }
71+
72+ callback ( errors: errors)
73+ }
74+
6075 func clearErrors( ) {
6176 self . errors = [ : ]
6277 }
Original file line number Diff line number Diff line change @@ -164,4 +164,18 @@ class ValidatorTests: XCTestCase {
164164 UNREGISTER_VALIDATOR . unregisterField ( UNREGISTER_TXT_FIELD)
165165 XCTAssert ( UNREGISTER_VALIDATOR . validations [ UNREGISTER_TXT_FIELD] == nil , " Textfield should unregister " )
166166 }
167+
168+ // MARK: Validate Functions
169+
170+ func testValidateWithCallback( ) {
171+ REGISTER_VALIDATOR . registerField ( REGISTER_TXT_FIELD, rules: [ EmailRule ( ) ] )
172+ REGISTER_TXT_FIELD . text = VALID_EMAIL
173+ REGISTER_VALIDATOR . validate { ( errors) -> Void in
174+ XCTAssert ( errors. count == 0 , " Should not come back with errors " )
175+ }
176+ REGISTER_TXT_FIELD . text = INVALID_EMAIL
177+ REGISTER_VALIDATOR . validate { ( errors) -> Void in
178+ XCTAssert ( errors. count == 1 , " Should come back with 1 error " )
179+ }
180+ }
167181}
You can’t perform that action at this time.
0 commit comments