Skip to content

Return successfully validated text fields as delegate parameter #70

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions SwiftValidator/Core/Validator.swift
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,13 @@ import UIKit

@objc public protocol ValidationDelegate {
func validationSuccessful()
func validationFailed(errors: [UITextField:ValidationError])
func validationFailed(errors: [UITextField:ValidationError], validFields: [UITextField])
}

public class Validator {
// dictionary to handle complex view hierarchies like dynamic tableview cells
public var errors = [UITextField:ValidationError]()
public var validFields = [UITextField]()
public var validations = [UITextField:ValidationRule]()
private var successStyleTransform:((validationRule:ValidationRule)->Void)?
private var errorStyleTransform:((validationError:ValidationError)->Void)?
Expand All @@ -28,6 +29,7 @@ public class Validator {
private func validateAllFields() {

errors = [:]
validFields = []

for (textField, rule) in validations {
if let error = rule.validateField() {
Expand All @@ -39,6 +41,8 @@ public class Validator {
}
} else {
// No error
validFields.append(textField)

// let the user transform the field if they want
if let transform = self.successStyleTransform {
transform(validationRule: rule)
Expand Down Expand Up @@ -74,7 +78,7 @@ public class Validator {
if errors.isEmpty {
delegate.validationSuccessful()
} else {
delegate.validationFailed(errors)
delegate.validationFailed(errors, validFields: validFields)
}
}

Expand Down
2 changes: 1 addition & 1 deletion Validator/ViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ class ViewController: UIViewController , ValidationDelegate, UITextFieldDelegate
self.presentViewController(alert, animated: true, completion: nil)

}
func validationFailed(errors:[UITextField:ValidationError]) {
func validationFailed(errors:[UITextField:ValidationError], validFields: [UITextField]) {
print("Validation FAILED!")
}

Expand Down