Minor style changes
This commit is contained in:
parent
b949078cef
commit
274556a578
|
|
@ -18,15 +18,10 @@ class ConfirmationRule: Rule {
|
|||
}
|
||||
|
||||
func validate(value: String) -> Bool {
|
||||
if self.confirmField.text == value {
|
||||
return true
|
||||
} else {
|
||||
return false
|
||||
}
|
||||
return confirmField.text == value
|
||||
}
|
||||
|
||||
func errorMessage() -> String {
|
||||
return "This field does not match"
|
||||
}
|
||||
|
||||
}
|
||||
|
|
@ -10,13 +10,11 @@ import Foundation
|
|||
|
||||
class EmailRule: Rule {
|
||||
|
||||
private let REGEX: String
|
||||
private let REGEX: String = "[A-Z0-9a-z._%+-]+@[A-Za-z0-9.-]+\\.[A-Za-z]{2,6}"
|
||||
|
||||
init(){
|
||||
self.REGEX = "[A-Z0-9a-z._%+-]+@[A-Za-z0-9.-]+\\.[A-Za-z]{2,6}"
|
||||
}
|
||||
init(){}
|
||||
|
||||
init(regex:String){
|
||||
init(regex: String){
|
||||
REGEX = regex
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -8,20 +8,15 @@
|
|||
|
||||
import Foundation
|
||||
|
||||
|
||||
class FullNameRule : Rule {
|
||||
class FullNameRule: Rule {
|
||||
|
||||
var message:String {
|
||||
return "Please provide a first & last name"
|
||||
}
|
||||
|
||||
func validate(value:String) -> Bool {
|
||||
|
||||
var nameArray:[String] = split(value) { $0 == " " }
|
||||
if nameArray.count >= 2 {
|
||||
return true
|
||||
}
|
||||
return false
|
||||
func validate(value: String) -> Bool {
|
||||
var nameArray: [String] = split(value) { $0 == " " }
|
||||
return nameArray.count >= 2
|
||||
}
|
||||
|
||||
func errorMessage() -> String {
|
||||
|
|
|
|||
|
|
@ -8,24 +8,18 @@
|
|||
|
||||
import Foundation
|
||||
|
||||
|
||||
class MinLengthRule : Rule {
|
||||
class MinLengthRule: Rule {
|
||||
|
||||
private let DEFAULT_MIN_LENGTH: Int
|
||||
private let DEFAULT_MIN_LENGTH: Int = 3
|
||||
|
||||
init(){
|
||||
DEFAULT_MIN_LENGTH = 3
|
||||
}
|
||||
init(){}
|
||||
|
||||
init(length:Int){
|
||||
init(length: Int){
|
||||
self.DEFAULT_MIN_LENGTH = length
|
||||
}
|
||||
|
||||
func validate(value: String) -> Bool {
|
||||
if countElements(value) < DEFAULT_MIN_LENGTH {
|
||||
return false
|
||||
}
|
||||
return true
|
||||
return countElements(value) > DEFAULT_MIN_LENGTH
|
||||
}
|
||||
|
||||
func errorMessage() -> String {
|
||||
|
|
|
|||
|
|
@ -8,7 +8,7 @@
|
|||
|
||||
import Foundation
|
||||
|
||||
class PasswordRule : Rule {
|
||||
class PasswordRule: Rule {
|
||||
|
||||
// Alternative Regexes
|
||||
|
||||
|
|
@ -20,13 +20,11 @@ class PasswordRule : Rule {
|
|||
|
||||
// 8 characters. one uppercase
|
||||
|
||||
private let REGEX: String
|
||||
private let REGEX: String = "^(?=.*?[A-Z]).{8,}$"
|
||||
|
||||
init(){
|
||||
self.REGEX = "^(?=.*?[A-Z]).{8,}$"
|
||||
}
|
||||
init(){}
|
||||
|
||||
init(regex:String){
|
||||
init(regex: String){
|
||||
self.REGEX = regex
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -8,7 +8,6 @@
|
|||
|
||||
import Foundation
|
||||
|
||||
|
||||
class RequiredRule: Rule {
|
||||
|
||||
init(){}
|
||||
|
|
@ -17,11 +16,8 @@ class RequiredRule: Rule {
|
|||
return "This field is required"
|
||||
}
|
||||
|
||||
func validate(value:String) -> Bool {
|
||||
if value.isEmpty {
|
||||
return false
|
||||
}
|
||||
return true
|
||||
func validate(value: String) -> Bool {
|
||||
return !value.isEmpty
|
||||
}
|
||||
|
||||
func errorMessage() -> String {
|
||||
|
|
|
|||
|
|
@ -9,6 +9,6 @@
|
|||
import Foundation
|
||||
|
||||
protocol Rule {
|
||||
func validate(value:String) -> Bool
|
||||
func validate(value: String) -> Bool
|
||||
func errorMessage() -> String
|
||||
}
|
||||
|
|
|
|||
|
|
@ -10,13 +10,12 @@ import Foundation
|
|||
import UIKit
|
||||
|
||||
class ValidationError {
|
||||
let textField:UITextField
|
||||
var errorLabel:UILabel?
|
||||
let errorMessage:String
|
||||
let textField: UITextField
|
||||
let errorMessage: String
|
||||
var errorLabel: UILabel?
|
||||
|
||||
init(textField:UITextField, error:String){
|
||||
init(textField: UITextField, error: String) {
|
||||
self.textField = textField
|
||||
self.errorMessage = error
|
||||
}
|
||||
|
||||
}
|
||||
|
|
@ -10,11 +10,11 @@ import Foundation
|
|||
import UIKit
|
||||
|
||||
class ValidationRule {
|
||||
var textField:UITextField
|
||||
var errorLabel:UILabel?
|
||||
var rules:[Rule] = []
|
||||
var textField: UITextField
|
||||
var errorLabel: UILabel?
|
||||
var rules: [Rule] = []
|
||||
|
||||
init(textField: UITextField, rules:[Rule], errorLabel:UILabel?){
|
||||
init(textField: UITextField, rules: [Rule], errorLabel: UILabel?){
|
||||
self.textField = textField
|
||||
self.errorLabel = errorLabel
|
||||
self.rules = rules
|
||||
|
|
@ -22,12 +22,10 @@ class ValidationRule {
|
|||
|
||||
func validateField() -> ValidationError? {
|
||||
for rule in rules {
|
||||
var isValid:Bool = rule.validate(textField.text)
|
||||
if !isValid {
|
||||
if !rule.validate(textField.text) {
|
||||
return ValidationError(textField: self.textField, error: rule.errorMessage())
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
}
|
||||
|
|
@ -11,32 +11,27 @@ import UIKit
|
|||
|
||||
@objc protocol ValidationDelegate {
|
||||
func validationWasSuccessful()
|
||||
func validationFailed(errors:[UITextField:ValidationError])
|
||||
func validationFailed(errors: [UITextField:ValidationError])
|
||||
}
|
||||
|
||||
class Validator {
|
||||
// dictionary to handle complex view hierarchies like dynamic tableview cells
|
||||
var errors:[UITextField:ValidationError] = [:]
|
||||
var validations:[UITextField:ValidationRule] = [:]
|
||||
var errors: [UITextField:ValidationError] = [:]
|
||||
var validations: [UITextField:ValidationRule] = [:]
|
||||
|
||||
init(){}
|
||||
|
||||
// MARK: Using Keys
|
||||
|
||||
func registerField(textField:UITextField, rules:[Rule]) {
|
||||
validations[textField] = ValidationRule(textField: textField, rules: rules, errorLabel: nil)
|
||||
func registerField(textField: UITextField, errorLabel: UILabel? = nil, rules: [Rule]) {
|
||||
validations[textField] = ValidationRule(textField: textField, rules: rules, errorLabel: errorLabel)
|
||||
}
|
||||
|
||||
func registerField(textField:UITextField, errorLabel:UILabel, rules:[Rule]) {
|
||||
validations[textField] = ValidationRule(textField: textField, rules:rules, errorLabel:errorLabel)
|
||||
}
|
||||
|
||||
func validateAll(delegate:ValidationDelegate) {
|
||||
|
||||
func validateAll(delegate: ValidationDelegate) {
|
||||
for field in validations.keys {
|
||||
if let currentRule:ValidationRule = validations[field] {
|
||||
if var error:ValidationError = currentRule.validateField() {
|
||||
if (currentRule.errorLabel != nil) {
|
||||
if let currentRule: ValidationRule = validations[field] {
|
||||
if var error: ValidationError = currentRule.validateField() {
|
||||
if currentRule.errorLabel != nil {
|
||||
error.errorLabel = currentRule.errorLabel
|
||||
}
|
||||
errors[field] = error
|
||||
|
|
@ -53,8 +48,7 @@ class Validator {
|
|||
}
|
||||
}
|
||||
|
||||
func clearErrors(){
|
||||
func clearErrors() {
|
||||
self.errors = [:]
|
||||
}
|
||||
|
||||
}
|
||||
|
|
@ -41,12 +41,6 @@ class ViewController: UIViewController , ValidationDelegate, UITextFieldDelegate
|
|||
|
||||
}
|
||||
|
||||
override func didReceiveMemoryWarning() {
|
||||
super.didReceiveMemoryWarning()
|
||||
// Dispose of any resources that can be recreated.
|
||||
|
||||
}
|
||||
|
||||
@IBAction func submitTapped(sender: AnyObject) {
|
||||
println("Validating...")
|
||||
self.clearErrors()
|
||||
|
|
@ -101,6 +95,4 @@ class ViewController: UIViewController , ValidationDelegate, UITextFieldDelegate
|
|||
func hideKeyboard(){
|
||||
self.view.endEditing(true)
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
|
@ -15,7 +15,8 @@ class ZipCodeRule: Rule {
|
|||
init(){
|
||||
self.REGEX = "\\d{5}"
|
||||
}
|
||||
init(regex:String){
|
||||
|
||||
init(regex: String){
|
||||
self.REGEX = regex
|
||||
}
|
||||
|
||||
|
|
@ -26,5 +27,4 @@ class ZipCodeRule: Rule {
|
|||
func errorMessage() -> String {
|
||||
return "Enter a valid 5 digit zipcode"
|
||||
}
|
||||
|
||||
}
|
||||
Loading…
Reference in New Issue