Skip to content
This repository has been archived by the owner on Apr 24, 2023. It is now read-only.

Customizations

Tomáš Mlynarič edited this page Jan 20, 2018 · 4 revisions

There are lots of ways how to tweak library to work your way!

Globally (for whole app)

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()
	);
    }
}

Locally (for specified field)

public final ValiFieldEmail email = new ValiFieldEmail("default nullable value", "Custom error message");

Specify your field by adding validators

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;
		}
	});

Predefined validators

Base

  • addVerifyFieldValidator
  • addCustomValidator

ValiFieldText

  • addExactLengthValidator
  • addMaxLengthValidator
  • addMinLengthValidator
  • addNotEmptyValidator
  • addPaternValidator
  • addRangeLengthValidator

(more coming in future versions 👍 )

Own validators

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;
	}
});

Own fields

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);
			}
		});
	}
}