All rules: init with optional user-defined message possible
This commit is contained in:
parent
ba9615cc85
commit
0edfb58cc4
|
|
@ -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
|
||||
}
|
||||
}
|
||||
|
|
@ -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
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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 {
|
||||
|
|
|
|||
Loading…
Reference in New Issue