-
Notifications
You must be signed in to change notification settings - Fork 460
Using QuickRules
Ragunath Jawahar edited this page Feb 26, 2016
·
3 revisions
QuickRule
is a shortcut for creating custom validation rules almost instantly. You can also assign a sequence
to it, in case you want the rule to be validated in the desired sequence. This is same as the sequence
attribute found in Saripaar annotations. To create your rule, you should extend from QuickRule
.
public class AllowEvenNumbersRule extends QuickRule<EditText> {
// Override this constructor ONLY if you want sequencing.
public AllowEvenNumbersRule(int sequence) {
super(sequence);
}
@Override
public boolean isValid(EditText editText) {
String integerString = editText.getText().toString().trim();
return integerString.matches("\\d+") && Integer.parseInt(integerString) % 2 == 0;
}
@Override
public String getMessage(Context context) {
return context.getString(R.string.message_even_number_expected);
}
}
After that, associate the View
with your QuickRule
in the Validator
.
validator.put(evenNumberEditText, new AllowEvenNumbersRule(1));