Adding public method validateField()
This commit is contained in:
parent
3645d74294
commit
dd7007d6ad
|
|
@ -47,6 +47,26 @@ public class Validator {
|
|||
}
|
||||
}
|
||||
|
||||
// MARK: Public functions
|
||||
|
||||
public func validateField(textField: UITextField, callback: (error:ValidationError?) -> Void){
|
||||
if let fieldRule = validations[textField] {
|
||||
if let error = fieldRule.validateField() {
|
||||
if let transform = self.errorStyleTransform {
|
||||
transform(validationError: error)
|
||||
}
|
||||
callback(error: error)
|
||||
} else {
|
||||
if let transform = self.successStyleTransform {
|
||||
transform(validationRule: fieldRule)
|
||||
}
|
||||
callback(error: nil)
|
||||
}
|
||||
} else {
|
||||
callback(error: nil)
|
||||
}
|
||||
}
|
||||
|
||||
// MARK: Using Keys
|
||||
|
||||
public func styleTransformers(success success:((validationRule:ValidationRule)->Void)?, error:((validationError:ValidationError)->Void)?) {
|
||||
|
|
|
|||
|
|
@ -78,4 +78,18 @@ class ViewController: UIViewController , ValidationDelegate, UITextFieldDelegate
|
|||
func hideKeyboard(){
|
||||
self.view.endEditing(true)
|
||||
}
|
||||
|
||||
// MARK: Validate single field
|
||||
// Don't forget to use UITextFieldDelegate
|
||||
func textFieldShouldReturn(textField: UITextField) -> Bool {
|
||||
validator.validateField(textField){ error in
|
||||
if error == nil {
|
||||
// Field validation was successful
|
||||
} else {
|
||||
// Validation error occurred
|
||||
}
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
||||
}
|
||||
Loading…
Reference in New Issue