Passive Validator
Passive Validator is one of the features contained in onelib, Passive Validator
was created with the aim of handling a single input process or one that must be validated with several rules. The way Passive Validator
works is by being extended to an Activity
, then you can add a validation listener and what message you want to display if the validation does not match by triggering validation checks.
Example Usage
The first step is to create an Activity
then edit the xml as needed. For example
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<androidx.core.widget.NestedScrollView
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="16dp">
<com.google.android.material.textfield.TextInputLayout
android:id="@+id/tilEmail"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Email"
app:boxBackgroundColor="@color/white"
android:textColorHint="@color/colorGray">
<com.google.android.material.textfield.TextInputEditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="textEmailAddress"
android:textColorHint="@color/colorGray" />
</com.google.android.material.textfield.TextInputLayout>
<com.google.android.material.textfield.TextInputLayout
android:id="@+id/tilPassword"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Password"
app:boxBackgroundColor="@color/white"
android:textColorHint="@color/colorGray"
app:passwordToggleEnabled="true">
<com.google.android.material.textfield.TextInputEditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="textPassword"
android:textColorHint="@color/colorGray" />
</com.google.android.material.textfield.TextInputLayout>
<com.google.android.material.textfield.TextInputLayout
android:id="@+id/tilPhone"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Phone"
app:boxBackgroundColor="@color/white"
android:textColorHint="@color/colorGray">
<com.google.android.material.textfield.TextInputEditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="phone"
android:textColorHint="@color/colorGray" />
</com.google.android.material.textfield.TextInputLayout>
<com.google.android.material.textfield.TextInputLayout
android:id="@+id/tilName"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Name"
app:boxBackgroundColor="@color/white"
android:textColorHint="@color/colorGray">
<com.google.android.material.textfield.TextInputEditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="textPersonName"
android:textColorHint="@color/colorGray" />
</com.google.android.material.textfield.TextInputLayout>
<com.google.android.material.textfield.TextInputLayout
android:id="@+id/tilLongName"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Long Name"
app:boxBackgroundColor="@color/white"
android:textColorHint="@color/colorGray">
<com.google.android.material.textfield.TextInputEditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="textPersonName"
android:textColorHint="@color/colorGray" />
</com.google.android.material.textfield.TextInputLayout>
<com.google.android.material.textfield.TextInputLayout
android:id="@+id/tilNotEmpty"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Not Empty"
app:boxBackgroundColor="@color/white"
android:textColorHint="@color/colorGray">
<com.google.android.material.textfield.TextInputEditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textColorHint="@color/colorGray" />
</com.google.android.material.textfield.TextInputLayout>
<androidx.appcompat.widget.AppCompatButton
android:id="@+id/btnValidation"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="24dp"
android:text="Validate" />
</LinearLayout>
</androidx.core.widget.NestedScrollView>
</LinearLayout>
Then, implement PassiveFormActivity<binding>
inside the activity that was created earlier. For example
class PassiveValidationActivity : PassiveFormActivity<ActivityPassiveValidationBinding>() {
override fun getViewBinding(): ActivityPassiveValidationBinding =
ActivityPassiveValidationBinding.inflate(layoutInflater)
override fun initUI() {}
override fun initAction() {
binding.btnValidation.onClick {
validate()
}
}
override fun initProcess() {}
override fun initObservers() {}
override fun onValidationSuccess() {
// Do something when validation success
}
override fun onValidationFailed() {
// Do something when validation failed
}
override fun setupValidation() {
with(binding) {
addValidation(
Validation(
tilEmail,
listOf(
notEmptyRule(getString(R.string.error_field_required)),
emailRule(getString(R.string.error_email_invalid))
)
),
Validation(
tilPassword, listOf(
notEmptyRule(getString(R.string.error_field_required)),
passwordRule(getString(R.string.error_password))
)
),
Validation(
tilPhone, listOf(
notEmptyRule(getString(R.string.error_field_required)),
minMaxLengthRule(
getString(R.string.error_phone_number),
MIN_LENGTH,
MAX_LENGTH
)
)
),
Validation(
tilName, listOf(
notEmptyRule(getString(R.string.error_field_required)),
alphabetOnlyRule(getString(R.string.error_alphabet))
)
),
Validation(
tilLongName, listOf(
alphabetSpaceOnly(getString(R.string.error_field_required))
)
),
Validation(
tilNotEmpty, listOf(
notEmptyRule(getString(R.string.error_empty))
)
)
)
}
}
}
Result
Last updated