You can use Saripaar's Mode.IMMEDIATE
in combination with TextWatcher
instances to accomplish what you are looking for.
- Use the
@Order
annotation one every view fields that have Saripaar annotations. This is required to use immediate validation mode. If you want to validate rules in a specific order, each saripaar annotation has a sequence
attribute. The sequence
attribute is optional, but very helpful if you want the rules to be validated in a specific sequence.
@Order(1)
@NotEmpty(sequence = 1)
@Email(seqence = 2)
private EditText mEmailEditText;
@Order(2)
@Password
private EditText mPasswordEditText;
Set the mode to immediate mValidator.setMode(Mode.IMMEDIATE)
Add TextWatcher
s / appropriate listeners to your widgets.
TextWatcher validationTextWatcher = new TextWatcher() {
@Override
void afterTextChanged(Editable s) {
mValidator.validate();
}
// Other methods to override ...
}
mEmailEditText.addTextChangedListener(validationTextWatcher);
mPasswordEditText.addTextChangedListener(validationTextWatcher);
- (Optional) There's a default
ViewValidatedAction
implementation that will automatically clear errors on EditText
fields when it is valid. If your form uses alternate error display mechanism / widgets, you can clear them by specifying your own ViewValidatedAction
instance.
mValidator.setViewValidatedAction(new ViewValidatedAction() {
@Override
void onAllRulesPassed(View view) {
// ... clear errors based on the view type
}
});
Disclosure: I'm the author of Saripaar