From ffe902dba932338e4ce4debf62310112757e75ae Mon Sep 17 00:00:00 2001 From: Georg Fischer Date: Tue, 5 May 2015 16:52:08 +0200 Subject: [PATCH] fix issue with errors showing up for unregistered fields --- Validator/Validator.swift | 1 + ValidatorTests/ValidatorTests.swift | 15 +++++++++++++++ 2 files changed, 16 insertions(+) diff --git a/Validator/Validator.swift b/Validator/Validator.swift index 28eed19..8702566 100644 --- a/Validator/Validator.swift +++ b/Validator/Validator.swift @@ -33,6 +33,7 @@ public class Validator { public func unregisterField(textField:UITextField) { validations.removeValueForKey(textField) + errors.removeValueForKey(textField) } public func validate(delegate:ValidationDelegate) { diff --git a/ValidatorTests/ValidatorTests.swift b/ValidatorTests/ValidatorTests.swift index 95c9095..b6101d4 100644 --- a/ValidatorTests/ValidatorTests.swift +++ b/ValidatorTests/ValidatorTests.swift @@ -39,6 +39,9 @@ class ValidatorTests: XCTestCase { let UNREGISTER_VALIDATOR = Validator() let UNREGISTER_RULES = [Rule]() + let UNREGISTER_ERRORS_TXT_FIELD = UITextField() + let UNREGISTER_ERRORS_VALIDATOR = Validator() + override func setUp() { super.setUp() // Put setup code here. This method is called before the invocation of each test method in the class. @@ -165,6 +168,18 @@ class ValidatorTests: XCTestCase { XCTAssert(UNREGISTER_VALIDATOR.validations[UNREGISTER_TXT_FIELD] == nil, "Textfield should unregister") } + func testUnregisterError(){ + UNREGISTER_ERRORS_VALIDATOR.registerField(UNREGISTER_ERRORS_TXT_FIELD, rules: [EmailRule()]) + UNREGISTER_ERRORS_TXT_FIELD.text = INVALID_EMAIL + UNREGISTER_ERRORS_VALIDATOR.validate { (errors) -> Void in + XCTAssert(errors.count == 1, "Should come back with errors") + } + UNREGISTER_ERRORS_VALIDATOR.unregisterField(UNREGISTER_ERRORS_TXT_FIELD) + UNREGISTER_ERRORS_VALIDATOR.validate { (errors) -> Void in + XCTAssert(errors.count == 0, "Should not come back with errors") + } + } + // MARK: Validate Functions func testValidateWithCallback() {