AwesomeValidation

repository·master·Indexed 22 days ago

https://github.com/thyrlian/awesomevalidation

An Android library for simplifying form validation with minimal boilerplate. It supports various validation styles including BASIC, COLORATION, UNDERLABEL, and TEXT_INPUT_LAYOUT. Developers can attach rules via regex, patterns, ranges, or custom logic using addValidation() and trigger the process with a single .validate() call.

Tokens
2K
Snippets
4
Records
5
Agent score
28%

What's inside AwesomeValidation

  1. How AwesomeValidation works

    master

    AwesomeValidation follows a three-step workflow to implement form validation in Android:

    1. Declare validation style: Initialize the AwesomeValidation object with a specific visual style (e.g., BASIC, COLORATION, UNDERLABEL, or TEXT_INPUT_LAYOUT).
    2. Add validations: Register specific rules (regex, patterns, or custom logic) for your input fields using addValidation().
    3. Set a trigger: Call .validate() at a specific point, such as a button click, to execute the validation logic.
  2. Install AwesomeValidation via Gradle

    master

    You can add AwesomeValidation to your Android project using either Maven Central or JitPack.

    Add the following to your module's build.gradle:

    Using JitPack

    1. Add the JitPack repository to your root build.gradle:
    2. Add the dependency to your module's build.gradle.
    // Maven Central
    dependencies {
        implementation 'com.basgeekball:awesome-validation:4.3'
    }
    
    // JitPack setup in root build.gradle
    allprojects {
        repositories {
            ...
            maven { url 'https://jitpack.io' }
        }
    }
    
    // JitPack dependency in module build.gradle
    dependencies {
        implementation 'com.github.thyrlian:AwesomeValidation:v4.3'
    }
  3. Configure AwesomeValidation styles

    master

    When initializing AwesomeValidation, you can choose from several styles and configure their appearance:

    • BASIC: Standard validation.
    • COLORATION: Uses colors to indicate error states. Use .setColor(int color) to change the error color (defaults to RED).
    • UNDERLABEL: Displays error messages below the input.
      • Mandatory: Call .setContext(Context context).
      • Optional: Use .setUnderlabelColorByResource(int resId) or .setUnderlabelColor(int color) to customize the error text color.
      • Note: This style currently does not support ConstraintLayout.
    • TEXT_INPUT_LAYOUT: Designed for Material Design TextInputLayout.
      • Usage: Pass the TextInputLayout object itself to addValidation() instead of the embedded EditText.
      • Optional: Use .setTextInputLayoutErrorTextAppearance(int resId) to customize the error appearance.

    Global Setting: You can disable the default behavior of automatically focusing the first failed input field by calling AwesomeValidation.disableAutoFocusOnFirstFailure().

    // BASIC style
    AwesomeValidation mAwesomeValidation = new AwesomeValidation(BASIC);
    
    // COLORATION style
    AwesomeValidation mAwesomeValidation = new AwesomeValidation(COLORATION);
    mAwesomeValidation.setColor(Color.YELLOW);
    
    // UNDERLABEL style
    AwesomeValidation mAwesomeValidation = new AwesomeValidation(UNDERLABEL);
    mAwesomeValidation.setContext(this);
    mAwesomeValidation.setUnderlabelColorByResource(android.R.color.holo_orange_light);
    
    // TEXT_INPUT_LAYOUT style
    AwesomeValidation mAwesomeValidation = new AwesomeValidation(TEXT_INPUT_LAYOUT);
    mAwesomeValidation.setTextInputLayoutErrorTextAppearance(R.style.TextInputLayoutErrorStyle);
    
    // Disable auto-focus on failure
    AwesomeValidation.disableAutoFocusOnFirstFailure();
  4. Trigger and clear validation

    master

    To execute the validation logic, call .validate() on your AwesomeValidation instance (typically inside a click listener). To remove all validation error information and reset the UI, call .clear().

    // Trigger validation
    findViewById(R.id.btn_done).setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            mAwesomeValidation.validate();
        }
    });
    
    // Clear validation errors
    findViewById(R.id.btn_clr).setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            mAwesomeValidation.clear();
        }
    });
  5. Add validations to fields

    master

    Use addValidation() to attach rules to your views. Supported rule types include:

    • Regex/Patterns: Pass a regex string, a java.util.regex.Pattern, or Android Patterns (e.g., android.util.Patterns.EMAIL_ADDRESS).
    • Ranges: Use Guava#Range (e.g., Range.closed(min, max)).
    • Confirmation: To validate that a field matches another (e.g., password confirmation), pass the ID of the target field as the second argument to addValidation().
    • Simple Custom Validation: Implement SimpleCustomValidation for logic that only needs to return a boolean based on the input string.
    • Advanced Custom Validation: Implement CustomValidation, CustomValidationCallback, and NewErrorReset for full control over how errors are displayed and cleared.

    Note for Fragments: When using AwesomeValidation in a Fragment, call .validate() inside onActivityCreated rather than onCreateView to ensure the lifecycle is handled correctly.

    // Regex and Patterns
    mAwesomeValidation.addValidation(activity, R.id.edt_name, "[a-zA-Z\\s]+", R.string.err_name);
    mAwesomeValidation.addValidation(activity, R.id.edt_email, android.util.Patterns.EMAIL_ADDRESS, R.string.err_email);
    
    // Ranges
    mAwesomeValidation.addValidation(activity, R.id.edt_year, Range.closed(1900, Calendar.getInstance().get(Calendar.YEAR)), R.string.err_year);
    
    // Confirmation (matches R.id.edt_password)
    mAwesomeValidation.addValidation(activity, R.id.edt_password_confirmation, R.id.edt_password, R.string.err_password_confirmation);
    
    // TextInputLayout (pass the layout, not the EditText)
    mAwesomeValidation.addValidation(activity, R.id.til_email, Patterns.EMAIL_ADDRESS, R.string.err_email);
    
    // Simple Custom Validation
    mAwesomeValidation.addValidation(activity, R.id.edt_birthday, new SimpleCustomValidation() {
        @Override
        public boolean compare(String input) {
            // return true if valid, false otherwise
            return true;
        }
    }, R.string.err_birth);