public class FormValidator extends Object
EditTexts or CompoundButtons (CheckBox, RadioButton, ...).
Validation process can be launched via runValidation(). FormValidator runs validation
process on its current assigned FormComponents that can be assigned to a
validator instance via following methods:
FormComponent.validate() upon each of them and notifies
OnFormListener that can be specified via
setOnFormListener(FormValidator.OnFormListener) about occurred errors and about validation
result.
All current assigned form components can be obtained via getComponents(). To obtain a
single component, call getComponent(int) with id of that component.
VALIDATION_MODE_BULK
Assigned form components are validated regardless of occurred errors
(OnFormListener.onValidationError(FormValidator, FormComponent))
and validation result is notified via
OnFormListener.onValidationResult(FormValidator, boolean)
after all form components has been validated.
VALIDATION_MODE_SINGLE
Assigned form components are validated until the first error occurs. Then the validation process
is stopped, OnFormListener.onValidationError(...) is fired followed by OnFormListener.onValidationResult(...)
with validation result set to false.
setValidationMode(int). The default one
is VALIDATION_MODE_BULK.| Modifier and Type | Class and Description |
|---|---|
static interface |
FormValidator.OnFormListener
Simple listener to receive callbacks when some validation errors occurs during form validation
process and also callback with the result of validation process.
|
static interface |
FormValidator.ValidationMode
Defines an annotation for determining set of allowed validation modes for
setValidationMode(int)
method. |
| Modifier and Type | Field and Description |
|---|---|
protected android.content.Context |
mContext
Context in which is this validator used.
|
protected android.view.View |
mFormView
View in which are placed all form component views.
|
static int |
VALIDATION_MODE_BULK
Mode used to validate all currently assigned
BaseFormComponents. |
static int |
VALIDATION_MODE_SINGLE
Unlike
VALIDATION_MODE_BULK, when this mode is used, validation will be running
until some validation error occurs. |
| Constructor and Description |
|---|
FormValidator(android.view.View formView)
Same as
FormValidator(View, int) with VALIDATION_MODE_BULK
validation mode. |
FormValidator(android.view.View formView,
int mode)
Creates a new instance of FormValidator for the given formView with the specified
validation mode.
|
| Modifier and Type | Method and Description |
|---|---|
FormValidator |
assignComponent(FormComponent component)
Assigns the given form component to this validator.
|
FormValidator |
assignComponents(FormComponent... components)
Assigns the given form components to this validator.
|
FormValidator |
assignComponentViews(int... viewIds)
Assigns the set of ids of views which should be validated by this validator.
|
void |
clearErrors()
Clears the current errors of all assigned components.
|
FormComponent |
findComponent(int componentId)
Returns an instance of FormComponent with the requested id.
|
FormComponent |
getComponent(int componentId)
Returns an instance of FormComponent with the requested id.
|
android.util.SparseArray<FormComponent> |
getComponents()
Returns the set of current form components of this validator.
|
android.content.Context |
getContext()
Returns the context obtained from the context wrapper during initialization of this validator.
|
int |
getValidationMode()
Returns the current validation mode of this validator.
|
protected boolean |
onAssignComponentForView(android.view.View componentView)
Invoked to assign a new form component to this validator for the specified componentView.
|
boolean |
runValidation()
Runs validation process upon the current set of form components.
|
void |
setOnFormListener(FormValidator.OnFormListener listener)
Registers a callback to be invoked when some validation error occurs during form validation
process or when validation process is finished with the validation result.
|
void |
setSubmitView(int id)
Same as
setSubmitView(View) for id of the submit view that will be searched for
in the form view. |
void |
setSubmitView(android.view.View submitView)
Sets the submit view for this validator.
|
void |
setValidationMode(int mode)
Sets validation mode for this validator.
|
public static final int VALIDATION_MODE_BULK
BaseFormComponents. If some validation
error occurs, FormValidator.OnFormListener.onValidationError(FormValidator, FormComponent) will be
fired with that particular component, but validation process will continue and
FormValidator.OnFormListener.onValidationResult(FormValidator, boolean) will be fired after all
form components has been validated with either true or false validation result.public static final int VALIDATION_MODE_SINGLE
VALIDATION_MODE_BULK, when this mode is used, validation will be running
until some validation error occurs. At that time FormValidator.OnFormListener.onValidationError(FormValidator, FormComponent)
will be fired with that particular component and FormValidator.OnFormListener.onValidationResult(FormValidator, boolean)
will be fired with false as validation result and whole validation process stops.protected final android.content.Context mContext
protected final android.view.View mFormView
public FormValidator(android.view.View formView)
FormValidator(View, int) with VALIDATION_MODE_BULK
validation mode.public FormValidator(android.view.View formView,
int mode)
formView - The view in which are placed all form component views.mode - The validation mode flag. One of VALIDATION_MODE_BULK or VALIDATION_MODE_SINGLE.public FormValidator assignComponents(FormComponent... components)
components - Form components which should be validated by this validator.public FormValidator assignComponent(FormComponent component)
component - Form component which should be validated by this validator.public FormValidator assignComponentViews(int... viewIds)
viewIds - The set of ids of desired views from the current context's root view for which
should be auto-created FormComponents and assigned to this validator. Type of
each created form component, will be resolved from the type of view.public boolean runValidation()
Note, that all current errors will be cleared via clearErrors() before
validation process starts.
True if validation was performed, false otherwise. This will
return true always if there are some form components to validate.public void clearErrors()
BaseFormComponent.clearError()protected boolean onAssignComponentForView(android.view.View componentView)
Note, that this is invoked in case when there was supplied a set of ids of views to this
validator via assignComponentViews(int...) for which should be created and assigned
form components. This will be invoked from runValidation() whenever there are view
ids that has not been processed yet.
This implementation does nothing.
componentView - The view for which to assign new form component that will be validated
by this validator.True to indicate that component for the view has been successfully assigned,
false otherwise.public android.content.Context getContext()
public void setSubmitView(int id)
setSubmitView(View) for id of the submit view that will be searched for
in the form view.id - An id of the desired submit view.public void setSubmitView(android.view.View submitView)
Note, that this will attach to the given submitView an instance of
View.OnClickListener, so any previous OnClickListener attached to this view will be
replaced.
submitView - The view, on which click should be validation executed.public void setOnFormListener(FormValidator.OnFormListener listener)
listener - Listener callback to register. May be null to remove the current one.public void setValidationMode(int mode)
mode - One of VALIDATION_MODE_BULK or VALIDATION_MODE_SINGLE.public int getValidationMode()
VALIDATION_MODE_BULK or VALIDATION_MODE_SINGLE.public android.util.SparseArray<FormComponent> getComponents()
public FormComponent getComponent(int componentId)
findComponent(int),
this will check only at the top of current components.componentId - An id of the desired form component to obtain.null if there
is no such a form component assigned to this validator.public FormComponent findComponent(int componentId)
componentId - An id of the desired form component to find.null if there
is no such a form component assigned to this validator or any of assigned form component groups
does not contain form component with such an id.