Skip to content

Commit cc0089d

Browse files
Changed to passing in transform functions for your text fields
1 parent afc15b0 commit cc0089d

File tree

5 files changed

+102
-99
lines changed

5 files changed

+102
-99
lines changed

Validator/ValidationError.swift

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,4 +18,10 @@ public class ValidationError {
1818
self.textField = textField
1919
self.errorMessage = error
2020
}
21+
22+
public init(textField:UITextField, errorLabel:UILabel?, error:String){
23+
self.textField = textField
24+
self.errorLabel = errorLabel
25+
self.errorMessage = error
26+
}
2127
}

Validator/ValidationRule.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ public class ValidationRule {
2323
public func validateField() -> ValidationError? {
2424
for rule in rules {
2525
if !rule.validate(textField.text) {
26-
return ValidationError(textField: self.textField, error: rule.errorMessage())
26+
return ValidationError(textField: self.textField, errorLabel:self.errorLabel, error: rule.errorMessage())
2727
}
2828
}
2929
return nil

Validator/Validator.swift

Lines changed: 39 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,46 @@ 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 shouldMarkTextFieldsInError:Bool = false
21+
private var successStyleTransform:((validationRule:ValidationRule)->Void)?
22+
private var errorStyleTransform:((validationError:ValidationError)->Void)?
2223

2324
public init(){}
2425

26+
// MARK: Private functions
27+
28+
private func clearErrors() {
29+
self.errors = [:]
30+
}
31+
32+
private func validateAllFields() {
33+
34+
for field in validations.keys {
35+
if let currentRule: ValidationRule = validations[field] {
36+
if var error: ValidationError = currentRule.validateField() {
37+
errors[field] = error
38+
39+
// let the user transform the field if they want
40+
if let transform = self.errorStyleTransform {
41+
transform(validationError: error)
42+
}
43+
} else {
44+
// No error
45+
// let the user transform the field if they want
46+
if let transform = self.successStyleTransform {
47+
transform(validationRule: currentRule)
48+
}
49+
}
50+
}
51+
}
52+
}
53+
2554
// MARK: Using Keys
2655

56+
public func styleTransformers(#success:((validationRule:ValidationRule)->Void)?, #error:((validationError:ValidationError)->Void)?) {
57+
self.successStyleTransform = success
58+
self.errorStyleTransform = error
59+
}
60+
2761
public func registerField(textField:UITextField, rules:[Rule]) {
2862
validations[textField] = ValidationRule(textField: textField, rules: rules, errorLabel: nil)
2963
}
@@ -37,39 +71,11 @@ public class Validator {
3771
errors.removeValueForKey(textField)
3872
}
3973

40-
private func markTextFieldAsInError(field:UITextField) {
41-
field.layer.borderColor = UIColor.redColor().CGColor
42-
field.layer.borderWidth = 1.0
43-
}
44-
45-
private func unmarkTextFieldAsInError(field:UITextField) {
46-
field.layer.borderColor = UIColor.clearColor().CGColor
47-
field.layer.borderWidth = 0.0
48-
}
49-
5074
public func validate(delegate:ValidationDelegate) {
5175

52-
for field in validations.keys {
53-
if let currentRule: ValidationRule = validations[field] {
54-
if var error: ValidationError = currentRule.validateField() {
55-
if let errorLabel = currentRule.errorLabel {
56-
errorLabel.text = error.errorMessage
57-
}
58-
if shouldMarkTextFieldsInError {
59-
self.markTextFieldAsInError(field)
60-
}
61-
errors[field] = error
62-
} else {
63-
errors.removeValueForKey(field)
64-
if let errorLabel = currentRule.errorLabel {
65-
errorLabel.text = nil
66-
}
67-
if shouldMarkTextFieldsInError {
68-
self.unmarkTextFieldAsInError(field)
69-
}
70-
}
71-
}
72-
}
76+
self.clearErrors()
77+
78+
self.validateAllFields()
7379

7480
if errors.isEmpty {
7581
delegate.validationSuccessful()
@@ -80,32 +86,8 @@ public class Validator {
8086

8187
public func validate(callback:(errors:[UITextField:ValidationError])->Void) -> Void {
8288

83-
for field in validations.keys {
84-
if let currentRule:ValidationRule = validations[field] {
85-
if var error:ValidationError = currentRule.validateField() {
86-
if let errorLabel = currentRule.errorLabel {
87-
errorLabel.text = error.errorMessage
88-
}
89-
if shouldMarkTextFieldsInError {
90-
self.markTextFieldAsInError(field)
91-
}
92-
errors[field] = error
93-
} else {
94-
errors.removeValueForKey(field)
95-
if let errorLabel = currentRule.errorLabel {
96-
errorLabel.text = nil
97-
}
98-
if shouldMarkTextFieldsInError {
99-
self.unmarkTextFieldAsInError(field)
100-
}
101-
}
102-
}
103-
}
89+
self.validateAllFields()
10490

10591
callback(errors: errors)
10692
}
107-
108-
func clearErrors() {
109-
self.errors = [:]
110-
}
11193
}

Validator/ViewController.swift

Lines changed: 41 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,21 @@ class ViewController: UIViewController , ValidationDelegate, UITextFieldDelegate
3333

3434
self.view.addGestureRecognizer(UITapGestureRecognizer(target: self, action: "hideKeyboard"))
3535

36+
validator.styleTransformers(success:{ (validationRule) -> Void in
37+
println("here")
38+
// clear error label
39+
validationRule.errorLabel?.hidden = true
40+
validationRule.textField.layer.borderColor = UIColor.clearColor().CGColor
41+
validationRule.textField.layer.borderWidth = 0.0
42+
43+
}, error:{ (validationError) -> Void in
44+
println("error")
45+
validationError.errorLabel?.hidden = false
46+
validationError.errorLabel?.text = validationError.errorMessage
47+
validationError.textField.layer.borderColor = UIColor.redColor().CGColor
48+
validationError.textField.layer.borderWidth = 1.0
49+
})
50+
3651
validator.registerField(fullNameTextField, errorLabel: fullNameErrorLabel , rules: [RequiredRule(), FullNameRule()])
3752
validator.registerField(emailTextField, errorLabel: emailErrorLabel, rules: [RequiredRule(), EmailRule()])
3853
validator.registerField(emailConfirmTextField, errorLabel: emailConfirmErrorLabel, rules: [RequiredRule(), ConfirmationRule(confirmField: emailTextField)])
@@ -42,7 +57,7 @@ class ViewController: UIViewController , ValidationDelegate, UITextFieldDelegate
4257

4358
@IBAction func submitTapped(sender: AnyObject) {
4459
println("Validating...")
45-
self.clearErrors()
60+
// self.clearErrors()
4661
validator.validate(self)
4762
}
4863

@@ -58,38 +73,38 @@ class ViewController: UIViewController , ValidationDelegate, UITextFieldDelegate
5873
}
5974
func validationFailed(errors:[UITextField:ValidationError]) {
6075
println("Validation FAILED!")
61-
self.setErrors()
76+
// self.setErrors()
6277
}
6378

6479
// MARK: Error Styling
6580

66-
func removeError(label:UILabel, textField:UITextField) {
67-
label.hidden = true
68-
textField.layer.borderWidth = 0.0
69-
}
81+
// func removeError(label:UILabel, textField:UITextField) {
82+
// label.hidden = true
83+
// textField.layer.borderWidth = 0.0
84+
// }
7085

71-
func removeAllErrors(){
72-
removeError(fullNameErrorLabel, textField: fullNameTextField)
73-
removeError(emailErrorLabel, textField: emailTextField)
74-
removeError(phoneNumberErrorLabel, textField: phoneNumberTextField)
75-
removeError(zipcodeErrorLabel, textField: zipcodeTextField)
76-
}
86+
// func removeAllErrors(){
87+
// removeError(fullNameErrorLabel, textField: fullNameTextField)
88+
// removeError(emailErrorLabel, textField: emailTextField)
89+
// removeError(phoneNumberErrorLabel, textField: phoneNumberTextField)
90+
// removeError(zipcodeErrorLabel, textField: zipcodeTextField)
91+
// }
7792

78-
private func setErrors(){
79-
for (field, error) in validator.errors {
80-
field.layer.borderColor = UIColor.redColor().CGColor
81-
field.layer.borderWidth = 1.0
82-
error.errorLabel?.text = error.errorMessage
83-
error.errorLabel?.hidden = false
84-
}
85-
}
93+
// private func setErrors(){
94+
// for (field, error) in validator.errors {
95+
// field.layer.borderColor = UIColor.redColor().CGColor
96+
// field.layer.borderWidth = 1.0
97+
// error.errorLabel?.text = error.errorMessage
98+
// error.errorLabel?.hidden = false
99+
// }
100+
// }
86101

87-
private func clearErrors(){
88-
for (field, error) in validator.errors {
89-
field.layer.borderWidth = 0.0
90-
error.errorLabel?.hidden = true
91-
}
92-
}
102+
// private func clearErrors(){
103+
// for (field, error) in validator.errors {
104+
// field.layer.borderWidth = 0.0
105+
// error.errorLabel?.hidden = true
106+
// }
107+
// }
93108

94109
func hideKeyboard(){
95110
self.view.endEditing(true)

ValidatorTests/ValidatorTests.swift

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -253,21 +253,21 @@ class ValidatorTests: XCTestCase {
253253
}
254254
}
255255

256-
func testTextFieldBorderColorSet() {
257-
REGISTER_VALIDATOR.registerField(REGISTER_TXT_FIELD, errorLabel: ERROR_LABEL, rules: [EmailRule()])
258-
REGISTER_TXT_FIELD.text = INVALID_EMAIL
259-
REGISTER_VALIDATOR.shouldMarkTextFieldsInError = true
260-
REGISTER_VALIDATOR.validate { (errors) -> Void in
261-
XCTAssert(errors.count == 1, "Should come back with errors")
262-
XCTAssert(CGColorEqualToColor(self.REGISTER_TXT_FIELD.layer.borderColor, UIColor.redColor().CGColor), "Color should be what it was set as")
263-
264-
self.REGISTER_TXT_FIELD.text = self.VALID_EMAIL
265-
self.REGISTER_VALIDATOR.validate { (errors) -> Void in
266-
XCTAssert(errors.count == 0, "Should come back without errors")
267-
XCTAssert(!CGColorEqualToColor(self.REGISTER_TXT_FIELD.layer.borderColor, UIColor.redColor().CGColor), "Color should be what it was set as")
268-
}
269-
}
270-
}
256+
// func testTextFieldBorderColorSet() {
257+
// REGISTER_VALIDATOR.registerField(REGISTER_TXT_FIELD, errorLabel: ERROR_LABEL, rules: [EmailRule()])
258+
// REGISTER_TXT_FIELD.text = INVALID_EMAIL
259+
// REGISTER_VALIDATOR.shouldMarkTextFieldsInError = true
260+
// REGISTER_VALIDATOR.validate { (errors) -> Void in
261+
// XCTAssert(errors.count == 1, "Should come back with errors")
262+
// XCTAssert(CGColorEqualToColor(self.REGISTER_TXT_FIELD.layer.borderColor, UIColor.redColor().CGColor), "Color should be what it was set as")
263+
//
264+
// self.REGISTER_TXT_FIELD.text = self.VALID_EMAIL
265+
// self.REGISTER_VALIDATOR.validate { (errors) -> Void in
266+
// XCTAssert(errors.count == 0, "Should come back without errors")
267+
// XCTAssert(!CGColorEqualToColor(self.REGISTER_TXT_FIELD.layer.borderColor, UIColor.redColor().CGColor), "Color should be what it was set as")
268+
// }
269+
// }
270+
// }
271271

272272
func testTextFieldBorderColorNotSet() {
273273
REGISTER_VALIDATOR.registerField(REGISTER_TXT_FIELD, errorLabel: ERROR_LABEL, rules: [EmailRule()])

0 commit comments

Comments
 (0)