From 21348cf08cfa5ff14f45cdd3bbd19ff61533c319 Mon Sep 17 00:00:00 2001 From: Jeff Potter Date: Sat, 7 Mar 2015 10:13:29 -0700 Subject: [PATCH 1/3] Update README.md --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index 6a5d07c..72a0739 100644 --- a/README.md +++ b/README.md @@ -99,6 +99,7 @@ class SSNVRule: Rule { } } +``` Credits ------- From 7168fe14da36f1120f1d644494f8d2703af51dd9 Mon Sep 17 00:00:00 2001 From: Jeff Potter Date: Wed, 11 Mar 2015 15:06:01 -0600 Subject: [PATCH 2/3] Update README.md --- README.md | 44 +++++++++++++++++++++++++++----------------- 1 file changed, 27 insertions(+), 17 deletions(-) diff --git a/README.md b/README.md index 72a0739..5d83379 100644 --- a/README.md +++ b/README.md @@ -3,15 +3,14 @@ Swift-Validator Swift Validator is a rule-based validation library for Swift. + ![Swift Validator](/swift-validator-v2.gif) ## Core Concepts -* ``UITextField`` + ``ValidationRule`` go into ```Validator`` -* ``ITextField`` + ``ValidationError`` come out of ```Validator`` -* ``UITextField`` is registered to ``Validator`` -* ``Validator`` evaluates ``ValidationRules`` sequentially and stops evaluating when a ``ValidationRule`` fails. -* Keys are used to allow field registration in TableViewControllers and complex view hierarchies +* ``UITextField`` + ``[Rule]`` + (and optional error ``UILabel``) go into ``Validator`` +* ``UITextField`` + ``ValidationError`` come out of ```Validator`` +* ``Validator`` evaluates ``[Rule]`` sequentially and stops evaluating when a ``Rule`` fails. ## Quick Start @@ -49,12 +48,18 @@ validator.registerField(zipcodeTextField, errorLabel: zipcodeErrorLabel, rules: ``` -Validate All Fields +Validate Fields on button tap or as the fields ```swift validator.validateAll(delegate:self) +``` + +Implement the Validation Delegate in your View controller + +```swift + // ValidationDelegate methods func validationWasSuccessful() { @@ -75,36 +80,41 @@ func validationFailed(errors:[UITextField:ValidationError]) { ## Custom Validation -We will create a ```SSNValidation``` 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. +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 Validation protocol +Create a class that implements the Rule protocol ```swift class SSNVRule: Rule { - let SSN_REGEX = "^\\d{3}-\\d{2}-\\d{4}$" + let REGEX = "^\\d{3}-\\d{2}-\\d{4}$" - func validate(value: String) -> (Bool, ValidationErrorType) { - if let ssnTest = NSPredicate(format: "SELF MATCHES %@", SSN_REGEX) { + 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, .NoError) + return true } - return (false, .SocialSecurity) // We will create this later ValidationErrorType.SocialSecurity + return false } - return (false, .SocialSecurity) } - + func errorMessage() -> String{ return "Not a valid SSN" } - } ``` Credits ------- -Swift Validator is written and maintained by Jeff Potter [@jpotts18](http://twitter.com/jpotts18) and friends. +Swift Validator is written and maintained by Jeff Potter [@jpotts18](http://twitter.com/jpotts18). ## Contributing From b6f8cd6941997455e76c3dc5b8f094fe1c8c5bbf Mon Sep 17 00:00:00 2001 From: Jeff Potter Date: Wed, 11 Mar 2015 17:17:49 -0600 Subject: [PATCH 3/3] Update README.md --- README.md | 53 ++++++++++++++++++++++++++--------------------------- 1 file changed, 26 insertions(+), 27 deletions(-) diff --git a/README.md b/README.md index 5d83379..c44ecb0 100644 --- a/README.md +++ b/README.md @@ -22,38 +22,37 @@ Initialize the ``Validator`` by setting a delegate to a View Controller or other let validator = Validator() -override func viewDidLoad() { - super.viewDidLoad() -} - ``` Register the fields that you want to validate ```swift +override func viewDidLoad() { + super.viewDidLoad() + + // Validation Rules are evaluated from left to right. + 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 validate against other fields using ConfirmRule + validator.registerField(emailConfirmTextField, errorLabel: emailConfirmErrorLabel, rules: [ConfirmationRule(confirmField: emailTextField)]) + + // You can now pass in regex and length parameters through overloaded contructors + validator.registerField(phoneNumberTextField, errorLabel: phoneNumberErrorLabel, rules: [RequiredRule(), MinLengthRule(length: 9)]) + validator.registerField(zipcodeTextField, errorLabel: zipcodeErrorLabel, rules: [RequiredRule(), ZipCodeRule(regex = "\\d{5}")]) -// Validation Rules are evaluated from left to right. -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 validate against other fields using ConfirmRule -validator.registerField(emailConfirmTextField, errorLabel: emailConfirmErrorLabel, rules: [ConfirmationRule(confirmField: emailTextField)]) - -// You can now pass in regex and length parameters through overloaded contructors -validator.registerField(phoneNumberTextField, errorLabel: phoneNumberErrorLabel, rules: [RequiredRule(), MinLengthRule(length: 9)]) -validator.registerField(zipcodeTextField, errorLabel: zipcodeErrorLabel, rules: [RequiredRule(), ZipCodeRule(regex = "\\d{5}")]) - +} ``` -Validate Fields on button tap or as the fields +Validate Fields on button tap or however you would like to trigger it. ```swift - -validator.validateAll(delegate:self) - +@IBAction func signupTapped(sender: AnyObject) { + validator.validateAll(delegate:self) +} ``` Implement the Validation Delegate in your View controller @@ -68,12 +67,12 @@ func validationWasSuccessful() { func validationFailed(errors:[UITextField:ValidationError]) { // turn the fields to red - for (field, error) in validator.errors { - field.layer.borderColor = UIColor.redColor().CGColor - field.layer.borderWidth = 1.0 - error.errorLabel?.text = error.errorMessage // works if you added labels - error.errorLabel?.hidden = false - } + for (field, error) in validator.errors { + field.layer.borderColor = UIColor.redColor().CGColor + field.layer.borderWidth = 1.0 + error.errorLabel?.text = error.errorMessage // works if you added labels + error.errorLabel?.hidden = false + } } ```