From 0edfb58cc442d89fe6776b2e3cbfd8795e22b515 Mon Sep 17 00:00:00 2001 From: mla2c Date: Mon, 1 Jun 2015 14:54:50 +0200 Subject: [PATCH] All rules: init with optional user-defined message possible --- Validator/ConfirmRule.swift | 6 ++++-- Validator/FloatRule.swift | 8 +++++--- Validator/FullNameRule.swift | 11 ++++++----- Validator/MaxLengthRule.swift | 8 +++++--- Validator/MinLengthRule.swift | 8 +++++--- Validator/RequiredRule.swift | 6 +++--- 6 files changed, 28 insertions(+), 19 deletions(-) diff --git a/Validator/ConfirmRule.swift b/Validator/ConfirmRule.swift index 46ff215..403d0bf 100644 --- a/Validator/ConfirmRule.swift +++ b/Validator/ConfirmRule.swift @@ -12,9 +12,11 @@ import UIKit public class ConfirmationRule: Rule { private let confirmField: UITextField + private var message : String - public init(confirmField: UITextField) { + public init(confirmField: UITextField, message : String = "This field does not match"){ self.confirmField = confirmField + self.message = message } public func validate(value: String) -> Bool { @@ -22,6 +24,6 @@ public class ConfirmationRule: Rule { } public func errorMessage() -> String { - return "This field does not match" + return message } } \ No newline at end of file diff --git a/Validator/FloatRule.swift b/Validator/FloatRule.swift index 4bf1c28..8e5c1f8 100644 --- a/Validator/FloatRule.swift +++ b/Validator/FloatRule.swift @@ -10,8 +10,10 @@ import Foundation public class FloatRule:Rule { - public init(){ - + private var message : String + + public init(message : String = "This must be a number with or without a decimal"){ + self.message = message } public func validate(value: String) -> Bool { @@ -24,6 +26,6 @@ public class FloatRule:Rule { } public func errorMessage() -> String { - return "This must be a number with or without a decimal" + return message } } \ No newline at end of file diff --git a/Validator/FullNameRule.swift b/Validator/FullNameRule.swift index a030214..9c79f53 100644 --- a/Validator/FullNameRule.swift +++ b/Validator/FullNameRule.swift @@ -11,16 +11,17 @@ import Foundation public class FullNameRule : Rule { - var message:String { - return "Please provide a first & last name" + private var message : String + + public init(message : String = "Please provide a first & last name"){ + self.message = message } - - public init(){} - + public func validate(value: String) -> Bool { var nameArray: [String] = split(value) { $0 == " " } return nameArray.count >= 2 } + public func errorMessage() -> String { return message } diff --git a/Validator/MaxLengthRule.swift b/Validator/MaxLengthRule.swift index f6b346a..fd29af1 100644 --- a/Validator/MaxLengthRule.swift +++ b/Validator/MaxLengthRule.swift @@ -10,18 +10,20 @@ import Foundation public class MaxLengthRule: Rule { private var DEFAULT_LENGTH: Int = 16 + private var message : String = "Must be at most 16 characters long" public init(){} - public init(length: Int){ + public init(length: Int, message : String = "Must be at most %ld characters long"){ self.DEFAULT_LENGTH = length + self.message = NSString(format: message, self.DEFAULT_LENGTH) as String } - + public func validate(value: String) -> Bool { return count(value) <= DEFAULT_LENGTH } public func errorMessage() -> String { - return "Must be at most \(DEFAULT_LENGTH) characters long" + return message } } diff --git a/Validator/MinLengthRule.swift b/Validator/MinLengthRule.swift index c5cf308..3ff63ea 100644 --- a/Validator/MinLengthRule.swift +++ b/Validator/MinLengthRule.swift @@ -11,11 +11,13 @@ import Foundation public class MinLengthRule: Rule { private var DEFAULT_LENGTH: Int = 3 - + private var message : String = "Must be at least 3 characters long" + public init(){} - public init(length: Int){ + public init(length: Int, message : String = "Must be at least %ld characters long"){ self.DEFAULT_LENGTH = length + self.message = NSString(format: message, self.DEFAULT_LENGTH) as String } public func validate(value: String) -> Bool { @@ -23,6 +25,6 @@ public class MinLengthRule: Rule { } public func errorMessage() -> String { - return "Must be at least \(DEFAULT_LENGTH) characters long" + return message } } diff --git a/Validator/RequiredRule.swift b/Validator/RequiredRule.swift index 50b2741..14e8e63 100644 --- a/Validator/RequiredRule.swift +++ b/Validator/RequiredRule.swift @@ -10,10 +10,10 @@ import Foundation public class RequiredRule: Rule { - public init(){} + private var message : String - var message: String { - return "This field is required" + public init(message : String = "This field is required"){ + self.message = message } public func validate(value: String) -> Bool {