V - Type of the view specific for this form component.I - Type of the input that can be presented within the component view.public abstract class BaseFormComponent<V extends android.view.View,I> extends Object implements FormComponent<V,I>
FormComponent implementation that can be used as base for specific form components
implementations.
This 'wrapper' is used by FormValidator that can be used to validate these input data.
The FormValidator will call validate() on each of its attached form components during the
validation process. If some error occurs, the FormValidator will fire error callback and if needed
a desired error can be set to the error component via FormComponent.setError(CharSequence). The form
validator to which is the form component currently attached can be accessed via getValidator().
Each form component must be created with its view of which validation will be then handled by that component. There are two base constructors for this purpose:
BaseFormComponent(int)
Use this constructor when you want to be a form view attached automatically by its form component when needed using its id.
BaseFormComponent(View)
Use this constructor when you have already access to a form view.
getView().| Modifier | Constructor and Description |
|---|---|
protected |
BaseFormComponent(int id)
Creates a new instance of base FormComponent.
|
protected |
BaseFormComponent(int id,
V view)
Creates a new instance of base FormComponent.
|
protected |
BaseFormComponent(V view)
Creates a new instance of base FormComponent.
|
| Modifier and Type | Method and Description |
|---|---|
void |
attachToValidator(FormValidator validator)
Attaches this form component to the given validator.
|
void |
clearError()
Clears the current error (if any) of this form component specified via
FormComponent.setError(CharSequence). |
FormComponent |
findComponent(int componentId)
Looks for a form component with the requested id.
|
protected FormComponent |
findComponentTraversal(int id)
Called immediately after
findComponent(int) if the passed componentId is
valid. |
int |
getId()
Returns the id of this component.
|
protected I |
getLastValidatedInput()
Returns the input that has been last time validated via
validate(). |
protected android.content.res.Resources |
getResources()
Returns the resources obtained form the current attached FormValidator.
|
FormValidator |
getValidator()
Returns the instance of
FormValidator to which is this form component currently attached. |
V |
getView()
Returns the view attached to this form component.
|
boolean |
hasError()
Checks whether this form component has error specified or not.
|
boolean |
hasInputChanged()
Checks whether the current value of this form component has changed since its last validation.
|
boolean |
isAttachedToValidator()
Returns flag indicating whether this form component is attached to FormValidator ot not.
|
protected String |
obtainString(int resId)
Wrapped
Resources.getString(int) (int)} on resources obtained from the current attached
FormValidator. |
protected CharSequence |
obtainText(int resId)
Wrapped
Resources.getText(int) on resources obtained from the current attached FormValidator. |
protected void |
onAttachedToValidator(FormValidator validator)
Invoked whenever this form component is assigned to the passed validator.
|
protected void |
onComponentViewAttached(V view)
Invoked only once, when there was
validate() called and the current component view is
null and here passed view was successfully obtained from the current
context root view hierarchy. |
protected abstract boolean |
onValidate(I input)
Invoked whenever
validate() is called. |
protected void |
resetCurrentState()
Called to reset the current state of this form component.
|
protected void |
setHasError(boolean hasError)
Sets a boolean flag indicating whether this form component has error set or not.
|
boolean |
validate()
Runs validation process of this form component.
|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitgetInput, setErrorprotected BaseFormComponent(int id)
The specified id will be used to find a view for this new form component via
FormValidator.findViewById(int) when appropriate.
id - The id for the new form component. Should be unique to correctly identify different
components within FormValidator or FormComponentsGroup.protected BaseFormComponent(@NonNull
V view)
Id of the specified view will be assigned also as id of this new form component.
view - The view for which is the new form component created.protected BaseFormComponent(int id,
@Nullable
V view)
id - The id for the new form component. Should be unique to correctly identify different
components within FormValidator or FormComponentsGroup.view - The view for which is the new form component created. If null the specified
id will be used to find such view via FormValidator.findViewById(int)
when appropriate.public void attachToValidator(@NonNull
FormValidator validator)
FormComponentattachToValidator in interface FormComponent<V extends android.view.View,I>validator - An instance of FormValidator with which is this form component associated.protected void onAttachedToValidator(@NonNull
FormValidator validator)
validator - An instance of FormValidator to which was this form component newly assigned.public boolean hasInputChanged()
FormComponent
Basically, whenever this returns true this component should be validated, otherwise
there is no need given the fact that its value did not change.
hasInputChanged in interface FormComponent<V extends android.view.View,I>True if the current value is different from the one that has been last time
validated via FormComponent.validate(), false otherwise.public boolean validate()
FormComponentvalidate in interface FormComponent<V extends android.view.View,I>True if the current state of this form component's view is valid,
false otherwise.protected void resetCurrentState()
validate()
before onValidate(Object) is called.@Nullable protected final I getLastValidatedInput()
validate().null if no validation has been performed yet.protected abstract boolean onValidate(@NonNull
I input)
validate() is called. Note, that there will be resetCurrentState()
always called before this.input - Current input of this form component to be validated.true if the current form component's view state passed
validation requirements, false otherwise.FormComponent.getInput()public final boolean isAttachedToValidator()
True if this form component is attached to FormValidator, false
otherwise.getValidator()@Nullable public final FormValidator getValidator()
FormValidator to which is this form component currently attached.null if this form component is not attached
to any FormValidator.isAttachedToValidator()public final int getId()
FormComponentgetId in interface FormComponent<V extends android.view.View,I>@Nullable public V getView()
FormComponentgetView in interface FormComponent<V extends android.view.View,I>public void clearError()
FormComponentFormComponent.setError(CharSequence).clearError in interface FormComponent<V extends android.view.View,I>protected final void setHasError(boolean hasError)
hasError - True if error is set, false otherwise.hasError()public boolean hasError()
FormComponenthasError in interface FormComponent<V extends android.view.View,I>True if error has been specified via FormComponent.setError(CharSequence),
false if not or has been cleared via FormComponent.clearError().@Nullable protected final android.content.res.Resources getResources()
null if this form component isn't
attached to any FormValidator.@NonNull protected final String obtainString(@StringRes int resId)
Resources.getString(int) (int)} on resources obtained from the current attached
FormValidator.@NonNull protected final CharSequence obtainText(@StringRes int resId)
Resources.getText(int) on resources obtained from the current attached FormValidator.@Nullable public final FormComponent findComponent(int componentId)
FormComponentfindComponent in interface FormComponent<V extends android.view.View,I>componentId - Id of the desired component to find.null if component with such a
componentId was not found.@Nullable protected FormComponent findComponentTraversal(int id)
findComponent(int) if the passed componentId is
valid. This can be useful if this component represents a group of form components.id - Id of the desired component to find.null
otherwise.protected void onComponentViewAttached(@NonNull
V view)
validate() called and the current component view is
null and here passed view was successfully obtained from the current
context root view hierarchy.view - The view obtained from the root view of the current context by the id
provided during initialization of this component.