Skip to content

gzeinnumer/AndroidFormValidation

Repository files navigation

AndroidFormValidation - Easy Validation


Simple way to Validate Form


Content List


Download

Add maven jitpack.io and dependencies in build.gradle (Project) :

// build.gradle project
allprojects {
  repositories {
    ...
    maven { url 'https://jitpack.io' }
  }
}

// build.gradle app/module
dependencies {
  ...
  implementation 'com.github.gzeinnumer:AndroidFormValidation:version'
  implementation 'com.google.android.material:material:1.2.0'
}

Feature List


Tech stack and 3rd library

  • Material.io (docs)

Usage

First Step. Use MaterialComponents in your style :

<style name="AppTheme" parent="Theme.MaterialComponents.Light.NoActionBar">
    <!-- Customize your theme here. -->
</style>

Validation Form

  • Import and start validation with make and object from class Validator.
//import com.gzeinnumer.afv.Validator;

TextInputLayout formNamaParent = findViewById(R.id.form_nama_p);
TextInputEditText formNama = findViewById(R.id.form_nama);
Button btnSubmit = findViewById(R.id.btn_submit);

Validator validator = new Validator();

//Add your form that you want to validate.

//Add `EditText` or `TextInputEditText` to `validator`.
validator.addView(formNama); // Default TypeForm.TEXT

btnSubmit.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        boolean result = validator.validate();
        //true if validate success
        //false if validate failed
        if (result) {
            Toast.makeText(this, "Done", Toast.LENGTH_SHORT).show();
        } else {
            Toast.makeText(this, "Not Done", Toast.LENGTH_SHORT).show();
        }
    }
});

  • Delete form view from validate process

Put your EditText or TextInputEditText to removeView(view).

validator.removeView(formNama);

  • Add view with custom configuration
//example 1
//Add `EditText` or `TextInputEditText` with custom `Rule`.
validator.addView(
    formNama,
    new Rule(TypeForm.EMAIL)
);

//example 2
//Add `EditText` or `TextInputEditText` with `TextInputLayout`.
validator.addView(
    new FormInput(formNamaParent, formNama)
); // Default TypeForm.TEXT

//example3
//Add `EditText` or `TextInputEditText` with `TextInputLayout` and custom `Rule`.
validator.addView(
    new FormInput(formNamaParent, formNama),
    new Rule(TypeForm.TEXT)
);

Read More Custom Rule

Example 1 Example 2 & Example 3

FullCode MainActivity & XML & Preview .

Validation Form RealTime

  • Import and start validation with make and object from class ValidatorRealTime.
TextInputEditText formUserName = = findViewById(R.id.form_username);
TextInputLayout formUserNameParent = findViewById(R.id.form_username_p);
Button btnSubmit = findViewById(R.id.btn_submit);

ValidatorRealTime validatorRealTime = new ValidatorRealTime();

validatorRealTime.addView(
    new FormInput(formUserNameParent, formUserName)
); // Default TypeForm.TEXT

validatorRealTime.build();

//Validate Realtime
validatorRealTime.observer(new ValidatorCallBack() {
    @Override
    public void result(boolean isDone) {
        //true if validate success
        //false if validate failed

        btnSubmit.setEnabled(isDone);
    }
});

//Trigger with onClickListener
btnValidate.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        boolean result = validatorRealTime.getResult();
        //true if validate success
        //false if validate failed
        if (result){
            Toast.makeText(SecondActivity.this, "Success", Toast.LENGTH_SHORT).show();
        } else {
            Toast.makeText(SecondActivity.this, "Failed", Toast.LENGTH_SHORT).show();
        }
    }
});

  • Delete form view from validate process

Put your EditText or TextInputEditText to removeView(view).

validatorRealTime.removeView(formNama);

  • Realtime message error when focus changed.

Put this code before build().

validatorRealTime.setEnableRealtimeMessageError(true); //default value is false

  • Add view with custom configuration
//example 1
//Add `EditText` or `TextInputEditText` with custom `Rule`.
validatorRealTime.addView(
    formUserName,
    new Rule(TypeForm.EMAIL)
);

//example 2
//Add `EditText` or `TextInputEditText` with `TextInputLayout`.
validatorRealTime.addView(
    new FormInput(formUserNameParent, formUserName)
); // Default TypeForm.TEXT

//example3
//Add `EditText` or `TextInputEditText` with `TextInputLayout` and custom `Rule`.
validatorRealTime.addView(
    new FormInput(formUserNameParent, formUserName),
    new Rule(TypeForm.TEXT)
);

Read More Custom Rule

FullCode SecondActivity & XML & Preview .

Get All EditText

ArrayList<EditText> v = new Validator().getAllEditText();

Example Code/app

Sample Code And App


Version

  • 1.0.0
    • First Release
  • 1.0.1
    • Add TEXT_NO_SYMBOL
  • 1.0.4
    • Support TextInputLayout
  • 1.0.5
    • Support Delete
  • 1.0.6
    • Bug Fixing
  • 1.1.6
    • Add Feature Disable Start With Space
  • 1.1.7
    • Bug Fixing
  • 2.0.0
    • Support SDK 16
  • 2.1.0
    • TEXT_NO_SYMBOL with permit some symbol
  • 2.1.1
    • Get All EditText
  • 2.1.2
    • Message

Contribution

You can sent your constibution to branch open-pull.


Copyright 2020 M. Fadli Zein

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Packages

No packages published

Languages