|
8 | 8 |
|
9 | 9 | import UIKit |
10 | 10 |
|
11 | | -class ViewController: UIViewController { |
| 11 | +class ViewController: UIViewController , ValidationDelegate { |
12 | 12 |
|
| 13 | + // TextFields |
| 14 | + @IBOutlet weak var fullNameTextField: UITextField! |
| 15 | + @IBOutlet weak var emailTextField: UITextField! |
| 16 | + @IBOutlet weak var phoneNumberTextField: UITextField! |
| 17 | + @IBOutlet weak var zipcodeTextField: UITextField! |
| 18 | + |
| 19 | + // Error Labels |
| 20 | + @IBOutlet weak var fullNameErrorLabel: UILabel! |
| 21 | + @IBOutlet weak var emailErrorLabel: UILabel! |
| 22 | + @IBOutlet weak var phoneNumberErrorLabel: UILabel! |
| 23 | + @IBOutlet weak var zipcodeErrorLabel: UILabel! |
| 24 | + |
| 25 | + let KEYS = ["Full Name", "Email", "Phone", "ZipCode"] |
| 26 | + |
| 27 | + let validator = Validator() |
| 28 | + |
13 | 29 | override func viewDidLoad() { |
14 | 30 | super.viewDidLoad() |
15 | | - // Do any additional setup after loading the view, typically from a nib. |
| 31 | + |
| 32 | + validator.registerFieldByKey(KEYS[0], textField: fullNameTextField, rules: [.Required, .FullName]) |
| 33 | + validator.registerFieldByKey(KEYS[1], textField: emailTextField, rules: [.Required, .Email]) |
| 34 | + validator.registerFieldByKey(KEYS[2], textField: phoneNumberTextField, rules: [.Required, .PhoneNumber]) |
| 35 | + validator.registerFieldByKey(KEYS[3], textField: zipcodeTextField, rules: [.Required, .ZipCode]) |
16 | 36 |
|
17 | | - var validator = Validator(delegate: self) |
18 | 37 | } |
19 | 38 |
|
20 | 39 | override func didReceiveMemoryWarning() { |
21 | 40 | super.didReceiveMemoryWarning() |
22 | 41 | // Dispose of any resources that can be recreated. |
23 | 42 |
|
24 | | - |
25 | 43 | } |
26 | 44 |
|
| 45 | + @IBAction func submitTapped(sender: AnyObject) { |
| 46 | + println("submit tapped") |
| 47 | + validator.validateAllKeys(self) |
| 48 | + } |
| 49 | + |
| 50 | + // MARK: ValidationDelegate Methods |
| 51 | + |
| 52 | + func validationFailed(errors: [String : ValidationError]) { |
| 53 | + |
| 54 | + if var fullNameError = errors[KEYS[0]] { |
| 55 | + setError(fullNameErrorLabel, error: fullNameError) |
| 56 | + } else { |
| 57 | + removeError(fullNameErrorLabel, textField: fullNameTextField) |
| 58 | + } |
| 59 | + |
| 60 | + if var emailNameError = errors[KEYS[1]] { |
| 61 | + setError(emailErrorLabel, error: emailNameError) |
| 62 | + } else { |
| 63 | + removeError(emailErrorLabel, textField: emailTextField) |
| 64 | + } |
| 65 | + |
| 66 | + if var phoneError = errors[KEYS[2]] { |
| 67 | + setError(phoneNumberErrorLabel, error: phoneError) |
| 68 | + } else { |
| 69 | + removeError(phoneNumberErrorLabel, textField: phoneNumberTextField) |
| 70 | + } |
| 71 | + |
| 72 | + if var zipError = errors[KEYS[3]] { |
| 73 | + setError(zipcodeErrorLabel, error: zipError) |
| 74 | + } else { |
| 75 | + removeError(zipcodeErrorLabel, textField: zipcodeTextField) |
| 76 | + } |
| 77 | + } |
| 78 | + |
| 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 | + |
| 90 | + func validationWasSuccessful() { |
| 91 | + |
| 92 | + } |
27 | 93 |
|
28 | 94 | } |
29 | 95 |
|
0 commit comments