VValidator (BETA)
View Validator, an easy-to-use form validation library for Kotlin & Android.
Gradle Dependency
Add this to your module's build.gradle
file:
The Basics
VValidator works automatically within any Activity or AndroidX Fragment.
The example above asserts that an edit text is not empty when a button a clicked. If that edit text
is not empty when the button is clicked, the callback that the comment is in is invoked.
Field Types
Input
The most basic type of supported view is an EditText
.
Input Layout
This is basically identical to input. However, this targets
TextInputLayout
views from the Google Material library. Errors
are shown differently by this view type and text is pulled
from the child TextInputEditText
rather than the parent.
Checkable
More specifically, a CompoundButton
. This includes Switch
,
RadioButton
, and CheckBox
views.
Spinner
A Spinner
is Android's core drop down view. It attaches to an
adapter, and shows a list of options when tapped.
Seeker
An AbsSeekBar
includes Android's core SeekBar
and RatingBar
views. They allow you to select
a number either with a horizontally sliding view or with horizontal icons.
Assertion Descriptions
All assertions expose a description(String)
method, used to specify a custom message for
assertion validation errors. They end up in the description
field of FieldError
instances.
All assertions provide default validation failure messages, however they may not be what you want
to display to your app users.
Validation Results
You get an instance of FormResult
through the submitWith(...)
callbacks. You can also get one
when you manually validate your form.
A call to validate()
goes through all of your fields, making all of the set assertions, and propagating
errors through onErrors
callbacks (which may or may not show errors in the UI automatically).
This result class gives you access to some detailed information.
Each instance of FieldError
contains additional information:
Error Handling
Input and Input Layout fields have default error handling because their underlying views have an
error property provided by Android. However, other view types do not. This library provides an
error hook for each field that you can use to display errors in the UI.
Submit With
You can have this library automatically handle validating your form with the click of a Button
:
Or even a MenuItem
:
Conditionals
You can apply assertions conditionally. Anything outside of a conditional
block is still always
executed during validation. This could be useful in many cases. One use case would be on fields that are optionally
visible. If a field is not visible, it should not be validated.
The conditional(..)
block above only asserts the field is a URL if a spinner's selection is greater
than 1. Say the spinner makes the input_site
field visible if its selection is > 1.
You can nest conditions as well:
You may have noticed somewhere above that input(...)
and inputLayout(...)
have an optional
argument named optional
. Internally, this uses conditional
to only apply inner assertions if
the input field's text is not empty.
Supporting Additional Views
If you need to support a view type that isn't supported out of the box, you can create custom
assertions and form fields.
First, you'd need an assertion class that goes with your view.
Then you'll need a custom FormField
class:
Finally, you can add an extension to Form
:
Now, you can use it:
When the form is validated, your assertion's isValid(MyView)
method is executed. If it returns
false, this view is marked as erroneous in the validation results.