Skip to content

Commit 29a5e8f

Browse files
committed
adding better readme
1 parent abc729a commit 29a5e8f

3 files changed

Lines changed: 18 additions & 13 deletions

File tree

README.md

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -37,15 +37,14 @@ Register the fields that you want to validate
3737
validator.registerField(fullNameTextField, rules: [RequiredRule(), FullNameRule()])
3838

3939
// You can pass in error labels with your rules
40-
4140
validator.registerField(emailTextField, errorLabel: emailErrorLabel, rules: [RequiredRule(), EmailRule()])
4241

43-
// You can validate against other fields
44-
validator.registerField(emailConfirmTextField, errorLabel: emailConfirmErrorLabel, rules: [RequiredRule(), EmailRule(), ConfirmationRule(confirmField: emailTextField)])
42+
// You can validate against other fields using ConfirmRule
43+
validator.registerField(emailConfirmTextField, errorLabel: emailConfirmErrorLabel, rules: [ConfirmationRule(confirmField: emailTextField)])
4544

46-
// You can now pass in regex and length parameters through overloaded contructors
45+
// You can now pass in regex and length parameters through overloaded contructors
4746
validator.registerField(phoneNumberTextField, errorLabel: phoneNumberErrorLabel, rules: [RequiredRule(), MinLengthRule(length: 9)])
48-
validator.registerField(zipcodeTextField, errorLabel: zipcodeErrorLabel, rules: [RequiredRule(), ZipCodeRule()])
47+
validator.registerField(zipcodeTextField, errorLabel: zipcodeErrorLabel, rules: [RequiredRule(), ZipCodeRule(regex = "\\d{5}")])
4948

5049
```
5150

@@ -54,20 +53,22 @@ Validate All Fields
5453

5554
```swift
5655

57-
validator.validateAllKeys(delegate:self)
56+
validator.validateAll(delegate:self)
5857

5958
// ValidationDelegate methods
6059

6160
func validationWasSuccessful() {
6261
// submit the form
6362
}
6463

65-
func validationFailed(errors:[String:ValidationError]){
64+
func validationFailed(errors:[UITextField:ValidationError]) {
6665
// turn the fields to red
67-
for error in errors.values {
68-
error.textField.backgroundColor = UIColor.redColor()
69-
println("error -> \(error.error.description)")
70-
}
66+
for (field, error) in validator.errors {
67+
field.layer.borderColor = UIColor.redColor().CGColor
68+
field.layer.borderWidth = 1.0
69+
error.errorLabel?.text = error.errorMessage // works if you added labels
70+
error.errorLabel?.hidden = false
71+
}
7172
}
7273

7374
```
@@ -80,7 +81,7 @@ Create a class that implements the Validation protocol
8081

8182
```swift
8283

83-
class SSNValidation: Validation {
84+
class SSNVRule: Rule {
8485
let SSN_REGEX = "^\\d{3}-\\d{2}-\\d{4}$"
8586

8687
func validate(value: String) -> (Bool, ValidationErrorType) {
@@ -92,6 +93,10 @@ class SSNValidation: Validation {
9293
}
9394
return (false, .SocialSecurity)
9495
}
96+
97+
func errorMessage() -> String{
98+
return "Not a valid SSN"
99+
}
95100

96101
}
97102

Validator/ViewController.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ class ViewController: UIViewController , ValidationDelegate, UITextFieldDelegate
3434

3535
validator.registerField(fullNameTextField, errorLabel: fullNameErrorLabel , rules: [RequiredRule(), FullNameRule()])
3636
validator.registerField(emailTextField, errorLabel: emailErrorLabel, rules: [RequiredRule(), EmailRule()])
37-
validator.registerField(emailConfirmTextField, errorLabel: emailConfirmErrorLabel, rules: [RequiredRule(), EmailRule(), ConfirmationRule(confirmField: emailTextField)])
37+
validator.registerField(emailConfirmTextField, errorLabel: emailConfirmErrorLabel, rules: [ConfirmationRule(confirmField: emailTextField)])
3838
validator.registerField(phoneNumberTextField, errorLabel: phoneNumberErrorLabel, rules: [RequiredRule(), MinLengthRule(length: 9)])
3939
validator.registerField(zipcodeTextField, errorLabel: zipcodeErrorLabel, rules: [RequiredRule(), ZipCodeRule()])
4040

0 commit comments

Comments
 (0)