public class Validator
extends java.lang.Object
Validator takes care of validating the
Views in the given controller instance. Usually, an
Activity or a Fragment. However, it can also be used
with other controller classes that contain references to View objects.
The Validator is capable of performing validations in two
modes,
Validator.Mode.BURST, where all the views are validated and all errors are reported
via the callback at once. Fields need not be ordered using the
Order annotation in BURST mode.
Validator.Mode.IMMEDIATE, in which the validation stops and the error is reported as soon
as a Rule fails. To use this mode, the fields SHOULD
BE ordered using the Order annotation.
There are three flavors of the validate() method.
validate(), no frills regular validation that validates all
Views.
validateTill(android.view.View), validates all Views till
the one that is specified.
validateBefore(android.view.View), validates all Views
before the specified View.
It is imperative that the fields are ordered while making the
validateTill(android.view.View) and validateBefore(android.view.View) method
calls.
The Validator requires a
Validator.ValidationListener that reports the outcome of the
validation.
Validator.ValidationListener.onValidationSucceeded()
is called if all Rules pass.
Validator.ValidationListener.onValidationFailed(java.util.List)
callback reports errors caused by failures. In Validator.Mode.IMMEDIATE this callback will
contain just one instance of the ValidationError
object.
| Modifier and Type | Class and Description |
|---|---|
static class |
Validator.Mode
Validation mode.
|
static interface |
Validator.ValidationListener
Listener with callback methods that notifies the outcome of validation.
|
static interface |
Validator.ViewValidatedAction
Interface that provides a callback when all
Rules
associated with a View passes. |
| Constructor and Description |
|---|
Validator(java.lang.Object controller)
Constructor.
|
| Modifier and Type | Method and Description |
|---|---|
boolean |
cancelAsync()
Cancels a running asynchronous validation task.
|
Validator.Mode |
getValidationMode()
Gets the current
Validator.Mode. |
boolean |
isValidating()
Used to find if an asynchronous validation task is running.
|
<VIEW extends android.view.View> |
put(VIEW view,
QuickRule<VIEW>... quickRules)
Add one or more
QuickRules for a View. |
<VIEW extends android.view.View,DATA_TYPE> |
registerAdapter(java.lang.Class<VIEW> viewType,
ViewDataAdapter<VIEW,DATA_TYPE> viewDataAdapter)
Registers a
ViewDataAdapter for the given
View. |
static void |
registerAnnotation(java.lang.Class<? extends java.lang.annotation.Annotation> ruleAnnotation)
A convenience method for registering
Rule annotations that
act on TextView and it's children, the most notable one being
EditText. |
static <VIEW extends android.view.View> |
registerAnnotation(java.lang.Class<? extends java.lang.annotation.Annotation> annotation,
java.lang.Class<VIEW> viewType,
ViewDataAdapter<VIEW,?> viewDataAdapter)
An elaborate method for registering custom rule annotations.
|
void |
removeRules(android.view.View view)
Remove all
Rules for the given View. |
void |
setValidationListener(Validator.ValidationListener validationListener)
Set a
Validator.ValidationListener to the
Validator. |
void |
setValidationMode(Validator.Mode validationMode)
Set the validation
Validator.Mode for the current
Validator instance. |
void |
setViewValidatedAction(Validator.ViewValidatedAction viewValidatedAction)
Set a
Validator.ViewValidatedAction to the
Validator. |
void |
validate()
Validates all
Views with Rules. |
void |
validate(boolean async)
Validates all
Views with Rules. |
void |
validateBefore(android.view.View view)
Validates all
Views before the specified View
parameter. |
void |
validateBefore(android.view.View view,
boolean async)
Validates all
Views before the specified View
parameter. |
void |
validateTill(android.view.View view)
Validates all
Views till the specified View
parameter. |
void |
validateTill(android.view.View view,
boolean async)
Validates all
Views till the specified View
parameter. |
public Validator(java.lang.Object controller)
controller - The class containing Views to be validated. Usually,
an Activity or a Fragment.public static void registerAnnotation(java.lang.Class<? extends java.lang.annotation.Annotation> ruleAnnotation)
Rule annotations that
act on TextView and it's children, the most notable one being
EditText. Register custom annotations for
TextViews that validates Double,
Float, Integer and String types.
For registering rule annotations for other view types see,
registerAdapter(Class, com.mobsandgeeks.saripaar.adapter.ViewDataAdapter).
ruleAnnotation - A rule Annotation.public static <VIEW extends android.view.View> void registerAnnotation(java.lang.Class<? extends java.lang.annotation.Annotation> annotation,
java.lang.Class<VIEW> viewType,
ViewDataAdapter<VIEW,?> viewDataAdapter)
VIEW - The View for which the
Annotation and
ViewDataAdapter is being registered.annotation - The annotation that you want to register.viewType - The View type.viewDataAdapter - An instance of the
ViewDataAdapter for your
View.public <VIEW extends android.view.View,DATA_TYPE> void registerAdapter(java.lang.Class<VIEW> viewType,
ViewDataAdapter<VIEW,DATA_TYPE> viewDataAdapter)
ViewDataAdapter for the given
View.VIEW - The View type.DATA_TYPE - The ViewDataAdapter type.viewType - The View for which a
ViewDataAdapter is being registered.viewDataAdapter - A ViewDataAdapter instance.public void setValidationListener(Validator.ValidationListener validationListener)
Validator.ValidationListener to the
Validator.validationListener - A Validator.ValidationListener
instance. null throws an IllegalArgumentException.public void setViewValidatedAction(Validator.ViewValidatedAction viewValidatedAction)
Validator.ViewValidatedAction to the
Validator.viewValidatedAction - A Validator.ViewValidatedAction
instance.public void setValidationMode(Validator.Mode validationMode)
Validator.Mode for the current
Validator instance.validationMode - Validator.Mode.BURST or Validator.Mode.IMMEDIATE, null throws an
IllegalArgumentException.public Validator.Mode getValidationMode()
Validator.Mode.Validator.public void validate()
Views with Rules.
When validating in Validator.Mode.IMMEDIATE, all
View fields must be ordered using the
Order annotation.public void validateBefore(android.view.View view)
Views before the specified View
parameter. View fields MUST be ordered using the
Order annotation.view - A View.public void validateTill(android.view.View view)
Views till the specified View
parameter. View fields MUST be ordered using the
Order annotation.view - A View.public void validate(boolean async)
Views with Rules.
When validating in Validator.Mode.IMMEDIATE, all
View fields must be ordered using the
Order annotation. Asynchronous calls will cancel
any pending or ongoing asynchronous validation and start a new one.async - true if asynchronous, false otherwise.public void validateBefore(android.view.View view,
boolean async)
Views before the specified View
parameter. View fields MUST be ordered using the
Order annotation. Asynchronous calls will cancel
any pending or ongoing asynchronous validation and start a new one.view - A View.async - true if asynchronous, false otherwise.public void validateTill(android.view.View view,
boolean async)
Views till the specified View
parameter. View fields MUST be ordered using the
Order annotation. Asynchronous calls will cancel
any pending or ongoing asynchronous validation and start a new one.view - A View.async - true if asynchronous, false otherwise.public boolean isValidating()
Validator in asynchronous mode.public boolean cancelAsync()
public <VIEW extends android.view.View> void put(VIEW view,
QuickRule<VIEW>... quickRules)
QuickRules for a View.public void removeRules(android.view.View view)
Rules for the given View.view - The View whose rules should be removed.