Skip to content

Commit

Permalink
rename validation listener
Browse files Browse the repository at this point in the history
  • Loading branch information
eddmash committed Nov 21, 2017
1 parent e77e4dd commit 3495dd2
Show file tree
Hide file tree
Showing 6 changed files with 116 additions and 70 deletions.
14 changes: 5 additions & 9 deletions docs/source/com/eddmash/validation/Validator.rst
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ Validator
.. java:package:: com.eddmash.validation
:noindex:

.. java:type:: public class Validator implements ValidationListener
.. java:type:: public class Validator implements ValidatorInterface
Performs view validation.

Expand Down Expand Up @@ -67,13 +67,13 @@ addCheck
addValidator
^^^^^^^^^^^^

.. java:method:: @Override public void addValidator(ValidationListener validator)
.. java:method:: @Override public void addValidator(ValidatorInterface validator)
:outertype: Validator

clearErrors
^^^^^^^^^^^

.. java:method:: public void clearErrors()
.. java:method:: @Override public void clearErrors()
:outertype: Validator

disableCheck
Expand All @@ -82,7 +82,7 @@ disableCheck
.. java:method:: @Override public void disableCheck(ValidationCheck validationCheck)
:outertype: Validator

\ :java:ref:`see <ValidationListener.addCheck(ValidationCheck)>`\
\ :java:ref:`see <ValidatorInterface.addCheck(ValidationCheck)>`\

disableSpinnerValidation
^^^^^^^^^^^^^^^^^^^^^^^^
Expand Down Expand Up @@ -111,13 +111,9 @@ disableValidation
getErrors
^^^^^^^^^

.. java:method:: public HashMap<String, List> getErrors()
.. java:method:: @Override public Map<String, List> getErrors()
:outertype: Validator

Returns all error that the validator found as a HashMap. with the key being tags if your passed in any when creating the validator otherwise all errors are returned under the tag NON_SPECIFIC

the value of the HashMap consists an ArrayList of errors that relate to each tag

getErrorsByTag
^^^^^^^^^^^^^^

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,23 +8,27 @@
.. java:import:: com.google.common.collect Range
ValidationListener
.. java:import:: java.util List
.. java:import:: java.util Map
ValidatorInterface
==================

.. java:package:: com.eddmash.validation
:noindex:

.. java:type:: public interface ValidationListener
.. java:type:: public interface ValidatorInterface
Enables validation of views in fragment find managed at the activity level see FarmerActivity and it fragments.
Class that is responsible for going through validation checks and determine if they are valid.

Methods
-------
addCheck
^^^^^^^^

.. java:method:: void addCheck(ValidationCheck validationCheck)
:outertype: ValidationListener
:outertype: ValidatorInterface

Adds validation checks to be enforced by a validator

Expand All @@ -33,18 +37,26 @@ addCheck
addValidator
^^^^^^^^^^^^

.. java:method:: void addValidator(ValidationListener validationListener)
:outertype: ValidationListener
.. java:method:: void addValidator(ValidatorInterface validatorInterface)
:outertype: ValidatorInterface

Add a validatorInterface object.

:param validatorInterface:

clearErrors
^^^^^^^^^^^

Add a validationListener object.
.. java:method:: void clearErrors()
:outertype: ValidatorInterface

:param validationListener:
Clear all the errors from the validator. maybe use when you have already run the validation onces and want to run the validation again using the same ValidatorInterface instance

disableCheck
^^^^^^^^^^^^

.. java:method:: void disableCheck(ValidationCheck validationCheck)
:outertype: ValidationListener
:outertype: ValidatorInterface

disable validation check

Expand All @@ -54,7 +66,7 @@ disableSpinnerValidation
^^^^^^^^^^^^^^^^^^^^^^^^

.. java:method:: void disableSpinnerValidation(View view)
:outertype: ValidationListener
:outertype: ValidatorInterface

Remove view from being validated.

Expand All @@ -64,7 +76,7 @@ disableSpinnerValidation
^^^^^^^^^^^^^^^^^^^^^^^^

.. java:method:: void disableSpinnerValidation(int id)
:outertype: ValidationListener
:outertype: ValidatorInterface

Remove view from being validated.

Expand All @@ -74,7 +86,7 @@ disableValidation
^^^^^^^^^^^^^^^^^

.. java:method:: void disableValidation(int id)
:outertype: ValidationListener
:outertype: ValidatorInterface

Remove view from being validated.

Expand All @@ -84,17 +96,29 @@ disableValidation
^^^^^^^^^^^^^^^^^

.. java:method:: void disableValidation(View view)
:outertype: ValidationListener
:outertype: ValidatorInterface

Remove view from being validated.

:param view:

getErrors
^^^^^^^^^

.. java:method:: Map<String, List> getErrors()
:outertype: ValidatorInterface

Returns all error that the validator found as a HashMap. with the key being tags if your passed in any when creating the validator otherwise all errors are returned under the tag NON_SPECIFIC

the value of the HashMap consists an ArrayList of errors that relate to each tag

:return: Map

setSpinnerValidation
^^^^^^^^^^^^^^^^^^^^

.. java:method:: void setSpinnerValidation(int form_province, String pattern, int form_err_blank)
:outertype: ValidationListener
:outertype: ValidatorInterface

Validates Spinners

Expand All @@ -106,7 +130,7 @@ setSpinnerValidation
^^^^^^^^^^^^^^^^^^^^

.. java:method:: void setSpinnerValidation(int form_province, String pattern, String form_err_blank)
:outertype: ValidationListener
:outertype: ValidatorInterface

Validates Spinners

Expand All @@ -118,7 +142,7 @@ setSpinnerValidation
^^^^^^^^^^^^^^^^^^^^

.. java:method:: void setSpinnerValidation(Spinner spinner, String pattern, int form_err_blank)
:outertype: ValidationListener
:outertype: ValidatorInterface

Validates Spinners

Expand All @@ -130,13 +154,13 @@ setSpinnerValidation
^^^^^^^^^^^^^^^^^^^^

.. java:method:: void setSpinnerValidation(Spinner spinner, String pattern, String form_err_blank)
:outertype: ValidationListener
:outertype: ValidatorInterface

setValidation
^^^^^^^^^^^^^

.. java:method:: void setValidation(EditText view, String pattern, String errorMsg)
:outertype: ValidationListener
:outertype: ValidatorInterface

Validates and edit box.

Expand All @@ -148,7 +172,7 @@ setValidation
^^^^^^^^^^^^^

.. java:method:: void setValidation(int view, String pattern, String errorMsg)
:outertype: ValidationListener
:outertype: ValidatorInterface

Validates and edit box.

Expand All @@ -160,7 +184,7 @@ setValidation
^^^^^^^^^^^^^

.. java:method:: void setValidation(int view, String pattern, int errorMsg)
:outertype: ValidationListener
:outertype: ValidatorInterface

Validates a view againsts the given context

Expand All @@ -172,19 +196,19 @@ setValidation
^^^^^^^^^^^^^

.. java:method:: void setValidation(EditText view, Range pattern, String errorMsg, boolean strict)
:outertype: ValidationListener
:outertype: ValidatorInterface

setValidation
^^^^^^^^^^^^^

.. java:method:: void setValidation(int view, Range pattern, String errorMsg, boolean strict)
:outertype: ValidationListener
:outertype: ValidatorInterface

validate
^^^^^^^^

.. java:method:: boolean validate()
:outertype: ValidationListener
:outertype: ValidatorInterface

Does the actual validation.

Expand Down
2 changes: 1 addition & 1 deletion docs/source/com/eddmash/validation/package-index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,6 @@ com.eddmash.validation
:maxdepth: 1

ErrorRenderer
ValidationListener
Validator
ValidatorInterface

21 changes: 17 additions & 4 deletions docs/source/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -51,18 +51,31 @@ Usage
Validate and Handle the errors
******************************
To run the validations invole the validators
:java:ref:`validate() <ValidatorInterface.validate()>` method.
This method returns ``true`` if the validation passed or ``false`` if the validations failed.
Incase of validation failure, the validation errors can be accessed via the
:java:ref:`getErrors() <ValidatorInterface.getErrors()>` method.
This library comes with a convenience :java:ref:`ErrorRenderer <ErrorRenderer>`, which can be used
to easily display the validation errors.
.. code-block:: java
errorSpace = (LinearLayout) findViewById(R.id.error_base);
// the layout where we display any validation errors
LinearLayout errorSpace = (LinearLayout) findViewById(R.id.error_base);
errorSpace.removeAllViews();// clear space first
if (validator.validate()) {
// .. code to perform if validation passes
} else {
// show the errors if validation failed
errorRenderer = new ErrorRenderer(this, validator);
// we use the renderer class to handle the display
ErrorRenderer errorRenderer = new ErrorRenderer(this, validator);
errorRenderer.render(errorSpace);
toggleShowErrors.setVisibility(View.VISIBLE);
}
46 changes: 20 additions & 26 deletions src/main/java/com/eddmash/validation/Validator.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,11 @@
/**
* Performs view validation.
*/
public class Validator implements ValidationListener {
public class Validator implements ValidatorInterface {
private static final String NOT_EMPTY = "(?m)^\\s*\\S+[\\s\\S]*$";
private static final String TELEPHONE = "(^\\+\\d+)?[0-9\\s()-]*";
private String _tag = "NON_SPECIFIC";
private List<ValidationListener> _validators = new ArrayList<>();
private List<ValidatorInterface> _validators = new ArrayList<>();
private List<Map> spinnerValidationList = new ArrayList<>();
private List<Map> edittextValidationList = new ArrayList<>();
private Activity context;
Expand All @@ -55,27 +55,6 @@ public Validator(String tag, Activity context) {
this._tag = tag;
}

/**
* Returns all error that the validator found as a HashMap.
* with the key being tags if your passed in any when creating the validator otherwise all
* errors are returned under the tag NON_SPECIFIC
* <p>
* the value of the HashMap consists an ArrayList of errors that relate to each tag
*
* @return
*/
public HashMap<String, List> getErrors() {
for (Object key : errorsCustomErrorsMsg.keySet()) {
if (errors.containsKey(key)) {
List values = errors.get(key);
values.addAll(errorsCustomErrorsMsg.get(key));
errors.put(key + "", values);
} else {
errors.put(key + "", errorsCustomErrorsMsg.get(key));
}
}
return errors;
}

/**
* Gets a list of errors for a specific tag.
Expand All @@ -92,7 +71,7 @@ public List getErrorsByTag(String tag) {


@Override
public void addValidator(ValidationListener validator) {
public void addValidator(ValidatorInterface validator) {
_validators.add(validator);
}

Expand Down Expand Up @@ -236,7 +215,7 @@ public void disableSpinnerValidation(int id) {
}

/**
* {@link ValidationListener#addCheck(ValidationCheck) see }
* {@link ValidatorInterface#addCheck(ValidationCheck) see }
*/
@Override
public void disableCheck(ValidationCheck validationCheck){
Expand Down Expand Up @@ -464,7 +443,7 @@ private boolean validateValidators() {
boolean validatorStatus;
Validator validator;

for (ValidationListener v : _validators) {
for (ValidatorInterface v : _validators) {
validator = (Validator) v;
validatorStatus = v.validate();
if (!validatorStatus) {
Expand All @@ -487,11 +466,26 @@ private void addCustomErrorsMsg(String string, List errorMessages) {
}
}

@Override
public void clearErrors() {
errorsCustomErrorsMsg.clear();
errors.clear();
}

@Override
public Map<String, List> getErrors() {
for (Object key : errorsCustomErrorsMsg.keySet()) {
if (errors.containsKey(key)) {
List values = errors.get(key);
values.addAll(errorsCustomErrorsMsg.get(key));
errors.put(key + "", values);
} else {
errors.put(key + "", errorsCustomErrorsMsg.get(key));
}
}
return errors;
}

@Override
public String toString() {
return "{ " + _tag + " = [" + spinnerValidationList + ", "
Expand Down

0 comments on commit 3495dd2

Please sign in to comment.