100 lines
3.8 KiB
Swift
100 lines
3.8 KiB
Swift
//
|
|
// ViewController.swift
|
|
// Validator
|
|
//
|
|
// Created by Jeff Potter on 11/20/14.
|
|
// Copyright (c) 2014 jpotts18. All rights reserved.
|
|
//
|
|
|
|
import Foundation
|
|
import UIKit
|
|
import SwiftValidator
|
|
|
|
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()
|
|
|
|
self.view.addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(ViewController.hideKeyboard)))
|
|
|
|
validator.styleTransformers(success:{ (validationRule) -> Void in
|
|
print("here")
|
|
// clear error label
|
|
validationRule.errorLabel?.isHidden = true
|
|
validationRule.errorLabel?.text = ""
|
|
if let textField = validationRule.field as? UITextField {
|
|
textField.layer.borderColor = UIColor.green.cgColor
|
|
textField.layer.borderWidth = 0.5
|
|
|
|
}
|
|
}, error:{ (validationError) -> Void in
|
|
print("error")
|
|
validationError.errorLabel?.isHidden = false
|
|
validationError.errorLabel?.text = validationError.errorMessage
|
|
if let textField = validationError.field as? UITextField {
|
|
textField.layer.borderColor = UIColor.red.cgColor
|
|
textField.layer.borderWidth = 1.0
|
|
}
|
|
})
|
|
|
|
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()])
|
|
}
|
|
|
|
@IBAction func submitTapped(_ sender: AnyObject) {
|
|
print("Validating...")
|
|
validator.validate(self)
|
|
}
|
|
|
|
// MARK: ValidationDelegate Methods
|
|
|
|
func validationSuccessful() {
|
|
print("Validation Success!")
|
|
let alert = UIAlertController(title: "Success", message: "You are validated!", preferredStyle: UIAlertController.Style.alert)
|
|
let defaultAction = UIAlertAction(title: "OK", style: .default, handler: nil)
|
|
alert.addAction(defaultAction)
|
|
self.present(alert, animated: true, completion: nil)
|
|
|
|
}
|
|
func validationFailed(_ errors:[(Validatable, ValidationError)]) {
|
|
print("Validation FAILED!")
|
|
}
|
|
|
|
@objc func hideKeyboard(){
|
|
self.view.endEditing(true)
|
|
}
|
|
|
|
// MARK: Validate single field
|
|
// Don't forget to use UITextFieldDelegate
|
|
func textFieldShouldReturn(_ textField: UITextField) -> Bool {
|
|
validator.validateField(textField){ error in
|
|
if error == nil {
|
|
// Field validation was successful
|
|
} else {
|
|
// Validation error occurred
|
|
}
|
|
}
|
|
return true
|
|
}
|
|
|
|
}
|