Android Validation using Saripaar v2

Published by Kapil Mohan on

Android Saripaar is a simple, feature-rich and powerful rule-based UI form validation library for Android. It is the SIMPLESTUI validation library available for Android.

Link:- https://github.com/ragunathjawahar/android-saripaar

dependencies {
    compile 'com.mobsandgeeks:android-saripaar:(latest version)'
}

Step 1 – Annotate your widgets using Saripaar Annotations

@NotEmpty
@Email
private EditText emailEditText;

@Password(min = 6, scheme = Password.Scheme.ALPHA_NUMERIC_MIXED_CASE_SYMBOLS)
private EditText passwordEditText;

@ConfirmPassword
private EditText confirmPasswordEditText;

@Checked(message = "You must agree to the terms.")
private CheckBox iAgreeCheckBox;

The annotations are self-explanatory. The @Order annotation is required ONLY when performing ordered validations usingValidator.validateTill(View) and Validator.validateBefore(View) or in IMMEDIATE mode.

Step 2 – Instantiate a new Validator

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    // Code…

    validator = new Validator(this);
    validator.setValidationListener(this);

    // More code…
}

You will need a Validator and a ValidationListener for receiving callbacks on validation events.

Step 3 – Implement a ValidationListener

public class RegistrationActivity extends Activity implements ValidationListener {

    // Code…

    @Override
    public void onValidationSucceeded() {
        Toast.makeText(this, "Yay! we got it right!", Toast.LENGTH_SHORT).show();
    }

    @Override
    public void onValidationFailed(List<ValidationError> errors) {
        for (ValidationError error : errors) {
            View view = error.getView();
            String message = error.getCollatedErrorMessage(this);

            // Display error messages ;)
            if (view instanceof EditText) {
                ((EditText) view).setError(message);
            } else {
                Toast.makeText(this, message, Toast.LENGTH_LONG).show();
            }
        }
    }
}
  • onValidationSucceeded() – Called when all your views pass all validations.
  • onValidationFailed(List<ValidationError> errors) – Called when there are validation error(s).

Step 4 – Validate

registerButton.setOnClickListener(new OnClickListener() {

    @Override
    public void onClick(View v) {
        validator.validate();
    }
});

Why Android Saripaar?

  • Built on top of Apache Commons Validator, a validation framework with proven track record on the web, desktop and mobile platforms.
  • Declarative style validation using Annotations.
  • Extensible, now allows Custom Annotations.
  • Synchronous and Asynchronous validations, you don’t have to worry about threading.
  • Supports both BURST and IMMEDIATE modes.
  • Works with Stock Android Widgets, no custom view dependencies.
  • Isolates validation logic using rules.
  • Compatible with other annotation-based libraries and frameworks such as ButterKnifeAndroidAnnotationsRoboGuice, etc.,

Link:- https://github.com/ragunathjawahar/android-saripaar

Library Details:-


Kapil Mohan

Like to add color to developers life and apps. Can create smile using Android, iOS, PHP, codeignitor, web technologies, etc... Feel free to contact me at [email protected] .