Skip to content

Commit 1949082

Browse files
Error fields auto-set error messages if given, can specify a different text field border color, can have the validator add the colored border to errored text fields
1 parent cc5e16a commit 1949082

File tree

2 files changed

+69
-2
lines changed

2 files changed

+69
-2
lines changed

Validator/Validator.swift

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ public class Validator {
1818
// dictionary to handle complex view hierarchies like dynamic tableview cells
1919
public var errors:[UITextField:ValidationError] = [:]
2020
public var validations:[UITextField:ValidationRule] = [:]
21+
public var markTextFieldsInError:Bool = false
22+
public var textFieldErrorColor:UIColor = UIColor.redColor()
2123

2224
public init(){}
2325

@@ -36,17 +38,28 @@ public class Validator {
3638
errors.removeValueForKey(textField)
3739
}
3840

41+
private func markTextFieldAsInError(field:UITextField) {
42+
field.layer.borderColor = self.textFieldErrorColor.CGColor
43+
field.layer.borderWidth = 1.0
44+
}
45+
3946
public func validate(delegate:ValidationDelegate) {
4047

4148
for field in validations.keys {
4249
if let currentRule: ValidationRule = validations[field] {
4350
if var error: ValidationError = currentRule.validateField() {
44-
if currentRule.errorLabel != nil {
45-
error.errorLabel = currentRule.errorLabel
51+
if let errorLabel = currentRule.errorLabel {
52+
errorLabel.text = error.errorMessage
53+
}
54+
if markTextFieldsInError {
55+
self.markTextFieldAsInError(field)
4656
}
4757
errors[field] = error
4858
} else {
4959
errors.removeValueForKey(field)
60+
if let errorLabel = currentRule.errorLabel {
61+
errorLabel.text = nil
62+
}
5063
}
5164
}
5265
}
@@ -63,9 +76,18 @@ public class Validator {
6376
for field in validations.keys {
6477
if let currentRule:ValidationRule = validations[field] {
6578
if var error:ValidationError = currentRule.validateField() {
79+
if let errorLabel = currentRule.errorLabel {
80+
errorLabel.text = error.errorMessage
81+
}
82+
if markTextFieldsInError {
83+
self.markTextFieldAsInError(field)
84+
}
6685
errors[field] = error
6786
} else {
6887
errors.removeValueForKey(field)
88+
if let errorLabel = currentRule.errorLabel {
89+
errorLabel.text = nil
90+
}
6991
}
7092
}
7193
}

ValidatorTests/ValidatorTests.swift

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,8 @@ class ValidatorTests: XCTestCase {
4242
let UNREGISTER_ERRORS_TXT_FIELD = UITextField()
4343
let UNREGISTER_ERRORS_VALIDATOR = Validator()
4444

45+
let ERROR_LABEL = UILabel()
46+
4547
override func setUp() {
4648
super.setUp()
4749
// Put setup code here. This method is called before the invocation of each test method in the class.
@@ -193,4 +195,47 @@ class ValidatorTests: XCTestCase {
193195
XCTAssert(errors.count == 1, "Should come back with 1 error")
194196
}
195197
}
198+
199+
// MARK: Validate error field gets it's text set to the error, if supplied
200+
201+
func testNoErrorMessageSet() {
202+
REGISTER_VALIDATOR.registerField(REGISTER_TXT_FIELD, errorLabel: ERROR_LABEL, rules: [EmailRule()])
203+
REGISTER_TXT_FIELD.text = VALID_EMAIL
204+
REGISTER_VALIDATOR.validate { (errors) -> Void in
205+
XCTAssert(errors.count == 0, "Should not come back with errors")
206+
XCTAssert(self.ERROR_LABEL.text == nil, "Shouldn't have an error message on the label")
207+
}
208+
}
209+
210+
func testErrorMessageSet() {
211+
REGISTER_VALIDATOR.registerField(REGISTER_TXT_FIELD, errorLabel: ERROR_LABEL, rules: [EmailRule()])
212+
REGISTER_TXT_FIELD.text = INVALID_EMAIL
213+
REGISTER_VALIDATOR.validate { (errors) -> Void in
214+
XCTAssert(errors.count == 1, "Should come back with errors")
215+
if let errorText = self.ERROR_LABEL.text {
216+
XCTAssert(errorText == errors[self.REGISTER_TXT_FIELD]!.errorMessage, "Shouldn't have an error message on the label, got: \(self.ERROR_LABEL.text!), expected: \(errors[self.REGISTER_TXT_FIELD]!.errorMessage)")
217+
}else{
218+
XCTAssert(false, "Error label should have text, not nil")
219+
}
220+
}
221+
}
222+
223+
func testErrorMessageSetAndThenUnset() {
224+
REGISTER_VALIDATOR.registerField(REGISTER_TXT_FIELD, errorLabel: ERROR_LABEL, rules: [EmailRule()])
225+
REGISTER_TXT_FIELD.text = INVALID_EMAIL
226+
REGISTER_VALIDATOR.validate { (errors) -> Void in
227+
XCTAssert(errors.count == 1, "Should come back with errors")
228+
if let errorText = self.ERROR_LABEL.text {
229+
XCTAssert(errorText == errors[self.REGISTER_TXT_FIELD]!.errorMessage, "Shouldn't have an error message on the label, got: \(self.ERROR_LABEL.text!), expected: \(errors[self.REGISTER_TXT_FIELD]!.errorMessage)")
230+
231+
self.REGISTER_TXT_FIELD.text = self.VALID_EMAIL
232+
self.REGISTER_VALIDATOR.validate { (errors) -> Void in
233+
XCTAssert(errors.count == 0, "Should not come back with errors")
234+
XCTAssert(self.ERROR_LABEL.text == nil, "Shouldn't have an error message on the label")
235+
}
236+
}else{
237+
XCTAssert(false, "Error label should have text, not nil")
238+
}
239+
}
240+
}
196241
}

0 commit comments

Comments
 (0)