Skip to content

Commit cc9f7af

Browse files
committed
Adding View Controller
1 parent 5c62b3a commit cc9f7af

1 file changed

Lines changed: 39 additions & 14 deletions

File tree

Validator/ViewController.swift

Lines changed: 39 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88

99
import UIKit
1010

11-
class ViewController: UIViewController , ValidationDelegate {
11+
class ViewController: UIViewController , ValidationDelegate, UITextFieldDelegate {
1212

1313
// TextFields
1414
@IBOutlet weak var fullNameTextField: UITextField!
@@ -28,7 +28,12 @@ class ViewController: UIViewController , ValidationDelegate {
2828

2929
override func viewDidLoad() {
3030
super.viewDidLoad()
31-
31+
32+
fullNameTextField.delegate = self
33+
emailTextField.delegate = self
34+
phoneNumberTextField.delegate = self
35+
zipcodeTextField.delegate = self
36+
3237
validator.registerFieldByKey(KEYS[0], textField: fullNameTextField, rules: [.Required, .FullName])
3338
validator.registerFieldByKey(KEYS[1], textField: emailTextField, rules: [.Required, .Email])
3439
validator.registerFieldByKey(KEYS[2], textField: phoneNumberTextField, rules: [.Required, .PhoneNumber])
@@ -43,14 +48,37 @@ class ViewController: UIViewController , ValidationDelegate {
4348
}
4449

4550
@IBAction func submitTapped(sender: AnyObject) {
46-
println("submit tapped")
51+
println("Validating...")
4752
validator.validateAllKeys(self)
4853
}
4954

55+
// MARK: Error Styling
56+
57+
func setError(label:UILabel, error:ValidationError) {
58+
label.hidden = false
59+
label.text = error.error.description()
60+
error.textField.layer.borderColor = UIColor.redColor().CGColor
61+
error.textField.layer.borderWidth = 2.0
62+
}
63+
64+
func removeError(label:UILabel, textField:UITextField) {
65+
label.hidden = true
66+
textField.layer.borderWidth = 0.0
67+
}
68+
69+
func removeAllErrors(){
70+
removeError(fullNameErrorLabel, textField: fullNameTextField)
71+
removeError(emailErrorLabel, textField: emailTextField)
72+
removeError(phoneNumberErrorLabel, textField: phoneNumberTextField)
73+
removeError(zipcodeErrorLabel, textField: zipcodeTextField)
74+
}
75+
5076
// MARK: ValidationDelegate Methods
5177

5278
func validationFailed(errors: [String : ValidationError]) {
5379

80+
println("Found \(errors.count) errors")
81+
5482
if var fullNameError = errors[KEYS[0]] {
5583
setError(fullNameErrorLabel, error: fullNameError)
5684
} else {
@@ -76,19 +104,16 @@ class ViewController: UIViewController , ValidationDelegate {
76104
}
77105
}
78106

79-
func setError(label:UILabel, error:ValidationError) {
80-
label.hidden = false
81-
label.text = error.error.description()
82-
error.textField.layer.borderColor = UIColor.redColor().CGColor
83-
error.textField.layer.borderWidth = 2.0
84-
}
85-
func removeError(label:UILabel, textField:UITextField) {
86-
label.hidden = true
87-
textField.layer.borderWidth = 0.0
88-
}
89-
90107
func validationWasSuccessful() {
91108

109+
println("Everything checks out!")
110+
111+
removeAllErrors()
112+
113+
var alert = UIAlertController(title: "Valid!", message: "Everything looks good to me", preferredStyle: UIAlertControllerStyle.Alert)
114+
alert.addAction(UIAlertAction(title: "Click", style: UIAlertActionStyle.Default, handler: nil))
115+
self.presentViewController(alert, animated: true, completion: nil)
116+
92117
}
93118

94119
}

0 commit comments

Comments
 (0)