The ValidationError class is used for representing errors of a failed validation. It contains the text field, error label, and error message of a failed validation.
Initializes a MaxLengthRule object that is to validate the length of the text of a text field
+- parameter length: Maximum character length.
+- parameter message: String of error message.
+- returns: An initialized object, or nil if an object could not be created for some reason that would not result in an exception.
+
+
+
+
Declaration
+
+
Swift
+
publicinit(length:Int,message:String="Must be at most %ld characters long")
+
+
+
+
+
Parameters
+
+
+
+
+
+ length
+
+
+
+
+
Maximum character length.
+
+
+
+
+
+
+
+ message
+
+
+
+
+
String of error message.
+
+
+
+
+
+
+
+
+
Return Value
+
An initialized object, or nil if an object could not be created for some reason that would not result in an exception.
Initializes a MaxLengthRule object that is to validate the length of the text of a text field
+- parameter length: Minimum character length.
+- parameter message: String of error message.
+- returns: An initialized MinLengthRule object, or nil if an object could not be created for some reason that would not result in an exception.
+
+
+
+
Declaration
+
+
Swift
+
publicinit(length:Int,message:String="Must be at least %ld characters long")
+
+
+
+
+
Parameters
+
+
+
+
+
+ length
+
+
+
+
+
Minimum character length.
+
+
+
+
+
+
+
+ message
+
+
+
+
+
String of error message.
+
+
+
+
+
+
+
+
+
Return Value
+
An initialized MinLengthRule object, or nil if an object could not be created for some reason that would not result in an exception.
Validates a text field.
+- parameter value: String to checked for validation.
+- returns: A boolean value. True if validation is successful; False if validation fails.
+
+
+
+
Declaration
+
+
Swift
+
publicfuncvalidate(value:String)->Bool
+
+
+
+
+
Parameters
+
+
+
+
+
+ value
+
+
+
+
+
String to checked for validation.
+
+
+
+
+
+
+
+
+
Return Value
+
A boolean value. True if validation is successful; False if validation fails.
Method used to initialize PhoneNumberRule object.
+- parameter message: Error message that is displayed if validation fails.
+- returns: An initialized PasswordRule object, or nil if an object could not be created for some reason that would not result in an exception.
+
+
+
+
Declaration
+
+
Swift
+
publicconvenienceinit(message:String="Enter a valid 10 digit phone number")
+
+
+
+
+
Parameters
+
+
+
+
+
+ message
+
+
+
+
+
Error message that is displayed if validation fails.
+
+
+
+
+
+
+
+
+
Return Value
+
An initialized PasswordRule object, or nil if an object could not be created for some reason that would not result in an exception.
The ValidationError class is used for representing errors of a failed validation. It contains the text field, error label, and error message of a failed validation.
Initializes ValidationError object with a textField and error.
+- parameter textField: UITextField that holds textField.
+- parameter errorMessage: String that holds error message.
+- returns: An initialized object, or nil if an object could not be created for some reason that would not result in an exception.
+
+
+
+
Declaration
+
+
Swift
+
publicinit(textField:UITextField,error:String)
+
+
+
+
+
Parameters
+
+
+
+
+
+ textField
+
+
+
+
+
UITextField that holds textField.
+
+
+
+
+
+
+
+ errorMessage
+
+
+
+
+
String that holds error message.
+
+
+
+
+
+
+
+
+
Return Value
+
An initialized object, or nil if an object could not be created for some reason that would not result in an exception.
Initializes ValidationError object with a textField, errorLabel, and errorMessage.
+- parameter textField: UITextField that holds textField.
+- parameter errorLabel: UILabel that holds error label.
+- parameter errorMessage: String that holds error message.
+- returns: An initialized object, or nil if an object could not be created for some reason that would not result in an exception.
Used to validate text field against its validation rules.
+- returns: ValidationError object if at least one error is found. Nil is returned if there are no validation errors.
+
+
+
+
Declaration
+
+
Swift
+
publicfuncvalidateField()->ValidationError?
+
+
+
+
+
Return Value
+
ValidationError object if at least one error is found. Nil is returned if there are no validation errors.
This method is used to validate a single field registered to Validator. If validation is unsuccessful,
+field gets added to errors dictionary.
+- parameter textField: Holds validator field data.
+- returns: No return value.
This method is used to style fields that have undergone validation checks. Success callback should be used to show common success styling and error callback should be used to show common error styling.
The ValidationError class is used for representing errors of a failed validation. It contains the text field, error label, and error message of a failed validation.
Initializes a MaxLengthRule object that is to validate the length of the text of a text field
+- parameter length: Maximum character length.
+- parameter message: String of error message.
+- returns: An initialized object, or nil if an object could not be created for some reason that would not result in an exception.
+
+
+
+
Declaration
+
+
Swift
+
publicinit(length:Int,message:String="Must be at most %ld characters long")
+
+
+
+
+
Parameters
+
+
+
+
+
+ length
+
+
+
+
+
Maximum character length.
+
+
+
+
+
+
+
+ message
+
+
+
+
+
String of error message.
+
+
+
+
+
+
+
+
+
Return Value
+
An initialized object, or nil if an object could not be created for some reason that would not result in an exception.
Initializes a MaxLengthRule object that is to validate the length of the text of a text field
+- parameter length: Minimum character length.
+- parameter message: String of error message.
+- returns: An initialized MinLengthRule object, or nil if an object could not be created for some reason that would not result in an exception.
+
+
+
+
Declaration
+
+
Swift
+
publicinit(length:Int,message:String="Must be at least %ld characters long")
+
+
+
+
+
Parameters
+
+
+
+
+
+ length
+
+
+
+
+
Minimum character length.
+
+
+
+
+
+
+
+ message
+
+
+
+
+
String of error message.
+
+
+
+
+
+
+
+
+
Return Value
+
An initialized MinLengthRule object, or nil if an object could not be created for some reason that would not result in an exception.
Validates a text field.
+- parameter value: String to checked for validation.
+- returns: A boolean value. True if validation is successful; False if validation fails.
+
+
+
+
Declaration
+
+
Swift
+
publicfuncvalidate(value:String)->Bool
+
+
+
+
+
Parameters
+
+
+
+
+
+ value
+
+
+
+
+
String to checked for validation.
+
+
+
+
+
+
+
+
+
Return Value
+
A boolean value. True if validation is successful; False if validation fails.
Method used to initialize PhoneNumberRule object.
+- parameter message: Error message that is displayed if validation fails.
+- returns: An initialized PasswordRule object, or nil if an object could not be created for some reason that would not result in an exception.
+
+
+
+
Declaration
+
+
Swift
+
publicconvenienceinit(message:String="Enter a valid 10 digit phone number")
+
+
+
+
+
Parameters
+
+
+
+
+
+ message
+
+
+
+
+
Error message that is displayed if validation fails.
+
+
+
+
+
+
+
+
+
Return Value
+
An initialized PasswordRule object, or nil if an object could not be created for some reason that would not result in an exception.
The ValidationError class is used for representing errors of a failed validation. It contains the text field, error label, and error message of a failed validation.
Initializes ValidationError object with a textField and error.
+- parameter textField: UITextField that holds textField.
+- parameter errorMessage: String that holds error message.
+- returns: An initialized object, or nil if an object could not be created for some reason that would not result in an exception.
+
+
+
+
Declaration
+
+
Swift
+
publicinit(textField:UITextField,error:String)
+
+
+
+
+
Parameters
+
+
+
+
+
+ textField
+
+
+
+
+
UITextField that holds textField.
+
+
+
+
+
+
+
+ errorMessage
+
+
+
+
+
String that holds error message.
+
+
+
+
+
+
+
+
+
Return Value
+
An initialized object, or nil if an object could not be created for some reason that would not result in an exception.
Initializes ValidationError object with a textField, errorLabel, and errorMessage.
+- parameter textField: UITextField that holds textField.
+- parameter errorLabel: UILabel that holds error label.
+- parameter errorMessage: String that holds error message.
+- returns: An initialized object, or nil if an object could not be created for some reason that would not result in an exception.
Used to validate text field against its validation rules.
+- returns: ValidationError object if at least one error is found. Nil is returned if there are no validation errors.
+
+
+
+
Declaration
+
+
Swift
+
publicfuncvalidateField()->ValidationError?
+
+
+
+
+
Return Value
+
ValidationError object if at least one error is found. Nil is returned if there are no validation errors.
This method is used to validate a single field registered to Validator. If validation is unsuccessful,
+field gets added to errors dictionary.
+- parameter textField: Holds validator field data.
+- returns: No return value.
This method is used to style fields that have undergone validation checks. Success callback should be used to show common success styling and error callback should be used to show common error styling.
overridefuncviewDidLoad(){
+ 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
+ // 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)])
+
+ // 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}")])
+
+ // You can unregister a text field if you no longer want to validate it
+ validator.unregisterField(fullNameTextField)
+}
+
+
+
Validate Fields on button tap or however you would like to trigger it.
Implement the Validation Delegate in your View controller
+
// ValidationDelegate methods
+
+funcvalidationSuccessful(){
+ // submit the form
+}
+
+funcvalidationFailed(errors:[UITextField:ValidationError]){
+ // turn the fields to red
+ for(field,error)invalidator.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
+ }
+}
+
+
+
Single Field Validation
+
+
You may use single field validation in some cases. This could be useful in situations such as controlling responders:
+
// Don't forget to use UITextFieldDelegate
+// and delegate yourTextField to self in viewDidLoad()
+functextFieldShouldReturn(textField:UITextField)->Bool{
+ validator.validateField(textField){errorin
+ iferror==nil{
+ // Field validation was successful
+ }else{
+ // Validation error occurred
+ }
+ }
+ returntrue
+}
+
+
Custom Validation
+
+
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.
overridefuncviewDidLoad(){
+ 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
+ // 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)])
+
+ // 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}")])
+
+ // You can unregister a text field if you no longer want to validate it
+ validator.unregisterField(fullNameTextField)
+}
+
+
+
Validate Fields on button tap or however you would like to trigger it.
Implement the Validation Delegate in your View controller
+
// ValidationDelegate methods
+
+funcvalidationSuccessful(){
+ // submit the form
+}
+
+funcvalidationFailed(errors:[UITextField:ValidationError]){
+ // turn the fields to red
+ for(field,error)invalidator.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
+ }
+}
+
+
+
Single Field Validation
+
+
You may use single field validation in some cases. This could be useful in situations such as controlling responders:
+
// Don't forget to use UITextFieldDelegate
+// and delegate yourTextField to self in viewDidLoad()
+functextFieldShouldReturn(textField:UITextField)->Bool{
+ validator.validateField(textField){errorin
+ iferror==nil{
+ // Field validation was successful
+ }else{
+ // Validation error occurred
+ }
+ }
+ returntrue
+}
+
+
Custom Validation
+
+
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.