Reactive Validator
Reactive Validator is one of the features contained in onelib, Reactive Validator
was created with the aim of handling a single input process or one that must be validated with several rules. The way Reactive 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:enabled="false"
android:text="Validate" />
</LinearLayout>
</androidx.core.widget.NestedScrollView>
</LinearLayout>
Then, implement ReactiveFormActivity<binding>
inside the activity that was created earlier. For example
class ReactiveValidationActivity : ReactiveFormActivity<ActivityReactiveValidationBinding>() {
companion object {
fun start(context: Context) {
context.startActivity(Intent(context, ReactiveValidationActivity::class.java))
}
}
override fun getViewBinding(): ActivityReactiveValidationBinding =
ActivityReactiveValidationBinding.inflate(layoutInflater)
override fun initUI() {}
override fun initAction() {}
override fun initProcess() {}
override fun initObservers() {}
override fun onValidationFailed() {
binding.btnValidation.disable()
}
override fun onValidationSuccess() {
binding.btnValidation.enable()
}
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))
)
),
Validation(
tilPhone, listOf(
notEmptyRule(getString(R.string.error_field_required)),
minMaxLengthRule(
getString(R.string.error_phone_number),
Constant.MIN_LENGTH,
Constant.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_field_required))
)
)
)
}
}
}
Result
Last updated