// // ViewController.swift // Validator // // Created by Jeff Potter on 11/20/14. // Copyright (c) 2014 jpotts18. All rights reserved. // import Foundation import UIKit class ViewController: UIViewController , ValidationDelegate, UITextFieldDelegate { // TextFields @IBOutlet weak var fullNameTextField: UITextField! @IBOutlet weak var emailTextField: UITextField! @IBOutlet weak var phoneNumberTextField: UITextField! @IBOutlet weak var zipcodeTextField: UITextField! @IBOutlet weak var emailConfirmTextField: UITextField! // Error Labels @IBOutlet weak var fullNameErrorLabel: UILabel! @IBOutlet weak var emailErrorLabel: UILabel! @IBOutlet weak var phoneNumberErrorLabel: UILabel! @IBOutlet weak var zipcodeErrorLabel: UILabel! @IBOutlet weak var emailConfirmErrorLabel: UILabel! let validator = Validator() override func viewDidLoad() { super.viewDidLoad() validator.registerField(fullNameTextField, errorLabel: fullNameErrorLabel , rules: [RequiredRule(), FullNameRule()]) validator.registerField(emailTextField, errorLabel: emailErrorLabel, rules: [RequiredRule(), EmailRule()]) validator.registerField(emailConfirmTextField, errorLabel: emailConfirmErrorLabel, rules: [RequiredRule(), ConfirmationRule(confirmField: emailTextField)]) validator.registerField(phoneNumberTextField, errorLabel: phoneNumberErrorLabel, rules: [RequiredRule(), MinLengthRule(length: 9)]) validator.registerField(zipcodeTextField, errorLabel: zipcodeErrorLabel, rules: [RequiredRule(), ZipCodeRule()]) } override func didReceiveMemoryWarning() { super.didReceiveMemoryWarning() // Dispose of any resources that can be recreated. } @IBAction func submitTapped(sender: AnyObject) { println("Validating...") self.clearErrors() validator.validateAll(self) } // MARK: Error Styling func removeError(label:UILabel, textField:UITextField) { label.hidden = true textField.layer.borderWidth = 0.0 } func removeAllErrors(){ removeError(fullNameErrorLabel, textField: fullNameTextField) removeError(emailErrorLabel, textField: emailTextField) removeError(phoneNumberErrorLabel, textField: phoneNumberTextField) removeError(zipcodeErrorLabel, textField: zipcodeTextField) } // MARK: ValidationDelegate Methods func validationWasSuccessful() { println("Validation Success!") var alert = UIAlertController(title: "Success", message: "You are validated!", preferredStyle: UIAlertControllerStyle.Alert) var defaultAction = UIAlertAction(title: "OK", style: .Default, handler: nil) alert.addAction(defaultAction) self.presentViewController(alert, animated: true, completion: nil) } func validationFailed(errors:[UITextField:ValidationError]) { println("Validation FAILED!") self.setErrors() } private func setErrors(){ for (field, error) in validator.errors { field.layer.borderColor = UIColor.redColor().CGColor field.layer.borderWidth = 1.0 error.errorLabel?.text = error.errorMessage error.errorLabel?.hidden = false } } private func clearErrors(){ for (field, error) in validator.errors { field.layer.borderWidth = 0.0 error.errorLabel?.hidden = true } } }