/** * Run validation on a single validator instance * * @param validator Validator to check * @return True if valid, false if not */ public boolean validateWith(@NonNull METValidator validator) { CharSequence text = getText(); boolean isValid = validator.isValid(text, text.length() == 0); if (!isValid) { setError(validator.getErrorMessage()); } postInvalidate(); return isValid; }
/** * Check all validators, sets the error text if not * * <p>NOTE: this stops at the first validator to report invalid. * * @return True if all validators pass, false if not */ public boolean validate() { if (validators == null || validators.isEmpty()) { return true; } CharSequence text = getText(); boolean isEmpty = text.length() == 0; boolean isValid = true; for (METValidator validator : validators) { //noinspection ConstantConditions isValid = isValid && validator.isValid(text, isEmpty); if (!isValid) { setError(validator.getErrorMessage()); break; } } if (isValid) { setError(null); } postInvalidate(); return isValid; }