Skip to content

Commit a685e16

Browse files
cleaned up code, fixed tests
1 parent cc0089d commit a685e16

File tree

3 files changed

+30
-68
lines changed

3 files changed

+30
-68
lines changed

Validator/Validator.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@ public class Validator {
3131

3232
private func validateAllFields() {
3333

34+
self.clearErrors()
35+
3436
for field in validations.keys {
3537
if let currentRule: ValidationRule = validations[field] {
3638
if var error: ValidationError = currentRule.validateField() {
@@ -73,8 +75,6 @@ public class Validator {
7375

7476
public func validate(delegate:ValidationDelegate) {
7577

76-
self.clearErrors()
77-
7878
self.validateAllFields()
7979

8080
if errors.isEmpty {

Validator/ViewController.swift

Lines changed: 3 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,9 @@ class ViewController: UIViewController , ValidationDelegate, UITextFieldDelegate
3737
println("here")
3838
// clear error label
3939
validationRule.errorLabel?.hidden = true
40-
validationRule.textField.layer.borderColor = UIColor.clearColor().CGColor
41-
validationRule.textField.layer.borderWidth = 0.0
40+
validationRule.errorLabel?.text = ""
41+
validationRule.textField.layer.borderColor = UIColor.greenColor().CGColor
42+
validationRule.textField.layer.borderWidth = 0.5
4243

4344
}, error:{ (validationError) -> Void in
4445
println("error")
@@ -57,7 +58,6 @@ class ViewController: UIViewController , ValidationDelegate, UITextFieldDelegate
5758

5859
@IBAction func submitTapped(sender: AnyObject) {
5960
println("Validating...")
60-
// self.clearErrors()
6161
validator.validate(self)
6262
}
6363

@@ -73,39 +73,8 @@ class ViewController: UIViewController , ValidationDelegate, UITextFieldDelegate
7373
}
7474
func validationFailed(errors:[UITextField:ValidationError]) {
7575
println("Validation FAILED!")
76-
// self.setErrors()
7776
}
7877

79-
// MARK: Error Styling
80-
81-
// func removeError(label:UILabel, textField:UITextField) {
82-
// label.hidden = true
83-
// textField.layer.borderWidth = 0.0
84-
// }
85-
86-
// func removeAllErrors(){
87-
// removeError(fullNameErrorLabel, textField: fullNameTextField)
88-
// removeError(emailErrorLabel, textField: emailTextField)
89-
// removeError(phoneNumberErrorLabel, textField: phoneNumberTextField)
90-
// removeError(zipcodeErrorLabel, textField: zipcodeTextField)
91-
// }
92-
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-
// }
101-
102-
// private func clearErrors(){
103-
// for (field, error) in validator.errors {
104-
// field.layer.borderWidth = 0.0
105-
// error.errorLabel?.hidden = true
106-
// }
107-
// }
108-
10978
func hideKeyboard(){
11079
self.view.endEditing(true)
11180
}

ValidatorTests/ValidatorTests.swift

Lines changed: 25 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -217,58 +217,51 @@ class ValidatorTests: XCTestCase {
217217
REGISTER_TXT_FIELD.text = VALID_EMAIL
218218
REGISTER_VALIDATOR.validate { (errors) -> Void in
219219
XCTAssert(errors.count == 0, "Should not come back with errors")
220-
XCTAssert(self.ERROR_LABEL.text == nil, "Shouldn't have an error message on the label")
221220
}
222221
}
223222

224223
func testErrorMessageSet() {
225224
REGISTER_VALIDATOR.registerField(REGISTER_TXT_FIELD, errorLabel: ERROR_LABEL, rules: [EmailRule()])
225+
var successCount = 0
226+
var errorCount = 0
227+
REGISTER_VALIDATOR.styleTransformers(success: { (validationRule) -> Void in
228+
successCount++
229+
}) { (validationError) -> Void in
230+
errorCount++
231+
}
226232
REGISTER_TXT_FIELD.text = INVALID_EMAIL
227233
REGISTER_VALIDATOR.validate { (errors) -> Void in
228234
XCTAssert(errors.count == 1, "Should come back with errors")
229-
if let errorText = self.ERROR_LABEL.text {
230-
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)")
231-
}else{
232-
XCTAssert(false, "Error label should have text, not nil")
233-
}
235+
XCTAssert(errorCount == 1, "Should have called the error style transform once")
236+
XCTAssert(successCount == 0, "Should not have called the success style transform as there are no successful fields")
234237
}
235238
}
236239

237240
func testErrorMessageSetAndThenUnset() {
238241
REGISTER_VALIDATOR.registerField(REGISTER_TXT_FIELD, errorLabel: ERROR_LABEL, rules: [EmailRule()])
242+
243+
var successCount = 0
244+
var errorCount = 0
245+
REGISTER_VALIDATOR.styleTransformers(success: { (validationRule) -> Void in
246+
successCount++
247+
}) { (validationError) -> Void in
248+
errorCount++
249+
}
250+
239251
REGISTER_TXT_FIELD.text = INVALID_EMAIL
240252
REGISTER_VALIDATOR.validate { (errors) -> Void in
241253
XCTAssert(errors.count == 1, "Should come back with errors")
242-
if let errorText = self.ERROR_LABEL.text {
243-
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)")
244-
245-
self.REGISTER_TXT_FIELD.text = self.VALID_EMAIL
246-
self.REGISTER_VALIDATOR.validate { (errors) -> Void in
247-
XCTAssert(errors.count == 0, "Should not come back with errors")
248-
XCTAssert(self.ERROR_LABEL.text == nil, "Shouldn't have an error message on the label")
249-
}
250-
}else{
251-
XCTAssert(false, "Error label should have text, not nil")
254+
XCTAssert(errorCount == 1, "Should have called the error style transform once")
255+
XCTAssert(successCount == 0, "Should not have called the success style transform as there are no successful fields")
256+
self.REGISTER_TXT_FIELD.text = self.VALID_EMAIL
257+
self.REGISTER_VALIDATOR.validate { (errors) -> Void in
258+
XCTAssert(errors.count == 0, "Should not come back with errors: \(errors)")
259+
XCTAssert(successCount == 1, "Should have called the success style transform once")
260+
XCTAssert(errorCount == 1, "Should not have called the error style transform again")
252261
}
253262
}
254263
}
255264

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-
// }
271-
272265
func testTextFieldBorderColorNotSet() {
273266
REGISTER_VALIDATOR.registerField(REGISTER_TXT_FIELD, errorLabel: ERROR_LABEL, rules: [EmailRule()])
274267
REGISTER_TXT_FIELD.text = INVALID_EMAIL

0 commit comments

Comments
 (0)