This repository has been archived by the owner on Apr 24, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 12
Customizations
Tomáš Mlynarič edited this page Jan 20, 2018
·
4 revisions
There are lots of ways how to tweak library to work your way!
public class MyApplication extends Application {
@Override
public void onCreate() {
ValiFi.install(this,
new ValiFi.Builder()
.setErrorDelay(1000) // possibility of .setErrorDelay(ValiFiErrorDelay.NEVER)
.setErrorResource(ValiFi.Builder.ERROR_RES_EMAIL, R.string.my_custom_email_error)
.setPattern(ValiFi.Builder.PATTERN_EMAIL, Patterns.EMAIL_ADDRESS)
.build()
);
}
}
public final ValiFieldEmail email = new ValiFieldEmail("default nullable value", "Custom error message");
public final ValiFieldText fieldWithDifferentValidations = new ValiFieldText();
fieldWithDifferentValidations
.addRangeLengthValidator(3, 10)
.setEmptyAllowed(true)
.addPatternValidator("pattern not valid", Patterns.IP_ADDRESS)
.addCustomValidator("custom not valid", new ValiFieldBase.PropertyValidator<String>() {
@Override
public boolean isValid(@Nullable String value) {
return whenThisIsValid;
}
});
- addVerifyFieldValidator
- addCustomValidator
- addExactLengthValidator
- addMaxLengthValidator
- addMinLengthValidator
- addNotEmptyValidator
- addPaternValidator
- addRangeLengthValidator
(more coming in future versions 👍 )
If you want to use your custom validator, just create a field and add custom validator:
public final ValiFieldText customField = new ValiFieldText();
customField.addCustomValidator(new ValiFieldBase.PropertyValidator<String>() {
@Override
public boolean isValid(@Nullable String value) {
// specify semantic for your validation
return false;
}
});
If you want to use your custom validation across all application, just inherit base field and add your app logic.
public class MyValiFieldCaptcha extends ValiFieldText {
public MyValiFieldCaptcha() {
super();
addMyValidator();
}
public MyValiFieldCaptcha(String defaultValue) {
super(defaultValue);
addMyValidator();
}
private void addMyValidator() {
addCustomValidator("Captcha must be correct", new PropertyValidator<String>() {
@Override
public boolean isValid(@Nullable String value) {
// custom validation rule
return "captcha".equals(value);
}
});
}
}