From ab2bb9802b08d9c9d2b263722a164c356ab520bb Mon Sep 17 00:00:00 2001 From: Leonardo Yvens Date: Sat, 30 May 2015 20:03:09 -0300 Subject: [PATCH] Update README.md Added custom message example. Sanitized SSN example. --- README.md | 32 +++++++++----------------------- 1 file changed, 9 insertions(+), 23 deletions(-) diff --git a/README.md b/README.md index b808b19..ba009ab 100644 --- a/README.md +++ b/README.md @@ -53,7 +53,8 @@ override func viewDidLoad() { validator.registerField(fullNameTextField, rules: [RequiredRule(), FullNameRule()]) // You can pass in error labels with your rules - validator.registerField(emailTextField, errorLabel: emailErrorLabel, rules: [RequiredRule(), EmailRule()]) + // You can pass in custom error messages to regex rules (such as ZipCodeRule and EmailRule) + validator.registerField(emailTextField, errorLabel: emailErrorLabel, rules: [RequiredRule(), EmailRule(message: "Invalid email")]) // You can validate against other fields using ConfirmRule validator.registerField(emailConfirmTextField, errorLabel: emailConfirmErrorLabel, rules: [ConfirmationRule(confirmField: emailTextField)]) @@ -101,31 +102,16 @@ func validationFailed(errors:[UITextField:ValidationError]) { We will create a ```SSNRule``` class to show how to create your own Validation. A United States Social Security Number (or SSN) is a field that consists of XXX-XX-XXXX. -Create a class that implements the Rule protocol +Create a class that inherits from RegexRule ```swift -class SSNVRule: Rule { - let REGEX = "^\\d{3}-\\d{2}-\\d{4}$" - - init(){} - - // allow for custom variables to be passed - init(regex:String){ - self.REGEX = regex - } - - func validate(value: String) -> Bool { - if let ssnTest = NSPredicate(format: "SELF MATCHES %@", REGEX) { - if ssnTest.evaluateWithObject(value) { - return true - } - return false - } - } - - func errorMessage() -> String{ - return "Not a valid SSN" +class SSNVRule: RegexRule { + + static let regex = "^\\d{3}-\\d{2}-\\d{4}$" + + convenience init(message : String = "Not a valid SSN"){ + self.init(regex: SSNVRule.regex, message : message) } } ```