Skip to content

Commit 62c0706

Browse files
committed
Merge pull request #20 from snorpey/deregister-textfield
add deregisterField method
2 parents f155284 + 5fcd1d1 commit 62c0706

3 files changed

Lines changed: 26 additions & 0 deletions

File tree

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,8 @@ override func viewDidLoad() {
4848
validator.registerField(phoneNumberTextField, errorLabel: phoneNumberErrorLabel, rules: [RequiredRule(), MinLengthRule(length: 9)])
4949
validator.registerField(zipcodeTextField, errorLabel: zipcodeErrorLabel, rules: [RequiredRule(), ZipCodeRule(regex = "\\d{5}")])
5050

51+
// You can unregister a text field if you no longer want to validate it
52+
validator.unregisterField(fullNameTextField)
5153
}
5254
```
5355

Validator/Validator.swift

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,10 @@ public class Validator {
3131
validations[textField] = ValidationRule(textField: textField, rules:rules, errorLabel:errorLabel)
3232
}
3333

34+
public func unregisterField(textField:UITextField) {
35+
validations.removeValueForKey(textField)
36+
}
37+
3438
public func validate(delegate:ValidationDelegate) {
3539

3640
for field in validations.keys {

ValidatorTests/ValidatorTests.swift

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,14 @@ class ValidatorTests: XCTestCase {
3131
let LEN_5 = "Howdy"
3232
let LEN_20 = "Paint the cat orange"
3333

34+
let REGISTER_TXT_FIELD = UITextField()
35+
let REGISTER_VALIDATOR = Validator()
36+
let REGISTER_RULES = [Rule]()
37+
38+
let UNREGISTER_TXT_FIELD = UITextField()
39+
let UNREGISTER_VALIDATOR = Validator()
40+
let UNREGISTER_RULES = [Rule]()
41+
3442
override func setUp() {
3543
super.setUp()
3644
// Put setup code here. This method is called before the invocation of each test method in the class.
@@ -144,4 +152,16 @@ class ValidatorTests: XCTestCase {
144152
XCTAssertFalse(FullNameRule().validate("Carl"), "Full Name should be invalid")
145153
}
146154

155+
// MARK: Register Field
156+
157+
func testRegisterField(){
158+
REGISTER_VALIDATOR.registerField(REGISTER_TXT_FIELD, rules: REGISTER_RULES)
159+
XCTAssert(REGISTER_VALIDATOR.validations[REGISTER_TXT_FIELD] != nil, "Textfield should register")
160+
}
161+
162+
func testUnregisterField(){
163+
UNREGISTER_VALIDATOR.registerField(UNREGISTER_TXT_FIELD, rules: UNREGISTER_RULES)
164+
UNREGISTER_VALIDATOR.unregisterField(UNREGISTER_TXT_FIELD)
165+
XCTAssert(UNREGISTER_VALIDATOR.validations[UNREGISTER_TXT_FIELD] == nil, "Textfield should unregister")
166+
}
147167
}

0 commit comments

Comments
 (0)