Merge pull request #33 from mla2c/master
All rules: init with optional user-defined message
This commit is contained in:
commit
f5376fbeee
|
|
@ -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
|
||||
}
|
||||
}
|
||||
|
|
@ -9,10 +9,11 @@
|
|||
import Foundation
|
||||
|
||||
public class EmailRule: RegexRule {
|
||||
|
||||
static let regex = "[A-Z0-9a-z._%+-]+@[A-Za-z0-9.-]+\\.[A-Za-z]{2,6}"
|
||||
|
||||
public convenience init(message : String = "Must be a valid email address"){
|
||||
self.init(regex: EmailRule.regex, message: message)
|
||||
}
|
||||
|
||||
|
||||
static let regex = "[A-Z0-9a-z._%+-]+@[A-Za-z0-9.-]+\\.[A-Za-z]{2,6}"
|
||||
|
||||
public convenience init(message : String = "Must be a valid email address"){
|
||||
self.init(regex: EmailRule.regex, message: message)
|
||||
}
|
||||
}
|
||||
|
|
@ -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
|
||||
}
|
||||
}
|
||||
|
|
@ -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
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -17,10 +17,11 @@ public class PasswordRule : RegexRule {
|
|||
//
|
||||
// no length. One uppercase. One lowercae. One number.
|
||||
// static let regex = "^(?=.*?[A-Z])(?=.*?[0-9])(?=.*?[a-z]).*?$"
|
||||
|
||||
|
||||
static let regex = "^(?=.*?[A-Z]).{8,}$"
|
||||
|
||||
public convenience init(message : String = "Must be 8 characters with 1 uppercase") {
|
||||
self.init(regex: PasswordRule.regex, message : message)
|
||||
public convenience init(message : String = "Must be 8 characters with 1 uppercase") {
|
||||
self.init(regex: PasswordRule.regex, message : message)
|
||||
|
||||
}
|
||||
}
|
||||
|
|
@ -12,10 +12,10 @@ public class RegexRule : Rule {
|
|||
|
||||
private var REGEX: String = "^(?=.*?[A-Z]).{8,}$"
|
||||
private var message : String
|
||||
|
||||
|
||||
public init(regex: String, message: String = "Invalid Regular Expression"){
|
||||
self.REGEX = regex
|
||||
self.message = message
|
||||
self.message = message
|
||||
}
|
||||
|
||||
public func validate(value: String) -> Bool {
|
||||
|
|
|
|||
|
|
@ -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 {
|
||||
|
|
|
|||
|
|
@ -10,7 +10,7 @@ import Foundation
|
|||
|
||||
public class ZipCodeRule: RegexRule {
|
||||
|
||||
public convenience init(message : String = "Enter a valid 5 digit zipcode"){
|
||||
self.init(regex: "\\d{5}", message : message)
|
||||
public convenience init(message : String = "Enter a valid 5 digit zipcode"){
|
||||
self.init(regex: "\\d{5}", message : message)
|
||||
}
|
||||
}
|
||||
Loading…
Reference in New Issue