Checks

Introduction

A Check is a class that implements the CheckInterface. The interface defines a few methods.

  • run() This is where all the logic of validation is placed. This method should return true if validation succeeds or false on failure.
  • getErrorMsg() this should the error message to be used incase validation fails.
  • setError() This method is invoked to with the error message above as an argument. This method should be used to set the error message on the editText.
  • clearError() This method is invoked just be validation is run. it should be used to clear any previous error displayed on the validator.

Creating custom validations

You can create a custom checks by implementing CheckInterface, or by extending one of the built-in checks.

As an example we create a simple check that checks if a string contains only alphanumeric characters.

class ContainsAlphaNumericCheck implements CheckInterface {
    private EditText editText;

    public ContainsAlphaNumericCheck(EditText editText) {
        this.editText = editText;
    }

    @Override
    public boolean run() {
        String value = editText.getText().toString();
        return Pattern.compile("^[a-zA-Z0-9]+$").matcher(value).matches();
    }

    @Override
    public String getErrorMsg() {
        return "String contains an illegal character: it can only contain letters or numbers.";
    }

    @Override
    public void setError(String error) {
        editText.setError(error);
    }

    @Override
    public void clearError() {
        editText.setError(null);
    }
}