HMOS library designed for rapid and customizable form validation.
You can use LibVerify with any Component that extends the original [TextField].
You just have to wrap your TextField with an InputValidator view. Example for an email and a custom regex :
<com.applib.libverify.InputValidator
ohos:width="wrap_content"
ohos:height="wrap_content"
app:required="true"
app:validator="isEmail"
app:requiredMessage="Email required">
<TextField
ohos:width="match_parent"
ohos:height="match_content"
ohos:inputType="textEmailAddress"
ohos:hint="Email"/>
</com.applib.libverify.InputValidator>
<com.applib.libverify.InputValidator
ohos:width="match_content"
ohos:height="match_content"
app:regex="^[0-9]{4}$"
app:errorMessage="4 digits only">
<TextField
ohos:width="match_content"
ohos:height="match_content"
ohos:hint="Regex 4 digits (custom error msg)"/>
</com.applib.libverify.InputValidator> Note: Be sure to add xmlns:app="http://schemas.huawei.com/hap/res-auto" next to xmlns:ohos for custom attributes to work.
InputValidator can be set to recognize email, phone, IP address, URL or number using validator attribute.
If you don't specify an errorMessage or a requiredMessage, predefined messages will be shown if the field is not valid.
Then validate your form :
// Initiate using context and a ComponentContainer that holds the InputValidators
// This example assumes a DirectionalLayout that contains the InputValidators
DirectionalLayout mComponentContainer =
(DirectionalLayout) findComponentById(ResourceTable.Id_form);
Form form = new Form.Builder(this, mComponentContainer)
.showErrors(true)
.build();
// validate the form
if(form.isValid()) {
// the form is valid
}
else {
// the form is not valid
} You can create programmatically InputValidator without passing by XML (see all Builder methods) :
// create the validator with the Builder
// emailTextField is the TextField to validate
// 'this' represents a Context
InputValidator emailValidator = new InputValidator.Builder(this)
.on(emailTextField)
.required(true)
.validatorType(InputValidator.IS_EMAIL)
.build();
// create the form and add the validator
Form form = new Form.Builder(this)
.addInputValidator(emailValidator)
.build();
// validate the form
if(form.isValid()) {
// the form is valid
}
else {
// the form is not valid
}You can create programmatically without using the Builders, but it is safer and quicker to use Builders.
All the attributes that can be used with InputValidator . They can be used in XML or in Java with setters :
| Attribute | Type | Description |
|---|---|---|
app:required |
boolean |
Whether the field is required or not |
app:validator |
enum |
Use a validator type predefined by FormValidator. You can use isEmail, isPhoneNumber, isNumeric, isUrl or isIP |
app:minLength |
int |
The minimum length of the field |
app:maxLength |
int |
The maximum length of the field |
app:minValue |
int |
The minimum value of the field (must be numeric) |
app:maxValue |
int |
The maximum value of the field (must be numeric) |
app:regex |
string |
Use a regex to validate a field |
app:identicalAs |
reference id |
The id of an TextField to which the field must be equal |
app:errorMessage |
string |
The message to display if the field is not valid |
app:requiredMessage |
string |
The message to display if the field is empty but was required. It implies that the field is required |
All the attributes that can be used with Form. They can be used in XML or in Java with setters :
| Attribute | Type | Default | Description |
|---|---|---|---|
app:showErrors |
boolean |
true |
Whether the errors must be shown on each TextField or not |
You can use a custom validator for an InputValidator :
// the InputValidator was present in the XML layout
InputValidator mInputValidator = (InputValidator) findComponentById(ResourceTable.Id_input_validator);
// your custom validator must extends AbstractValidator class
mInputValidator.setCustomValidator(new AbstractValidator() {
@Override
public boolean isValid(String value) {
return value.equals("ok man");
}
@Override
public String getErrorMessage() {
return "This field must be equals to 'ok man'";
}
});
// or create your InputValidator with the Builder
InputValidator inputValidator = new InputValidator.Builder(this)
.on(aTextField)
.customValidator(new AbstractValidator() {
@Override
public boolean isValid(String value) {
return value.equals("ok man");
}
@Override
public String getErrorMessage() {
return "This field must be equals to 'ok man'";
}
});
.build();If you want, you can use a Form component directly in XML. This view extends DirectionalLayout. It must wrap all the fields you want to check.
It can be useful for these reasons :
- You don't have to instantiate a
Formobject before validate the form - It will be easier to identify a form in your XML layout
- You can use two different and independent forms in the same XML layout
<!-- form1 -->
<com.applib.libverify.Form
ohos:id="$+id:form1"
ohos:orientation="vertical"
ohos:width="match_parent"
ohos:height="match_content"
ohos:padding="16fp">
<!-- email -->
<com.applib.libverify.InputValidator
ohos:width="match_content"
ohos:height="match_content">
<TextField
ohos:width="match_parent"
ohos:height="match_content"
ohos:text_size="20fp"
ohos:hint="Email"/>
</com.applib.libverify.InputValidator>
<!-- password -->
<com.applib.libverify.InputValidator
ohos:width="match_content"
ohos:height="match_content"
app:required="true"
app:minLength="6">
<TextField
ohos:width="match_parent"
ohos:height="match_content"
ohos:text_input_type="pattern_password"
ohos:text_size="20fp"
ohos:hint="Password (6 char. min) *" />
</com.applib.libverify.InputValidator>
<!-- submit form -->
<Button
ohos:id="$+id:validate_form1"
ohos:width="match_content"
ohos:height="match_content"
ohos:text_size="20fp"
ohos:text="Validate form1"/>
</com.applib.libverify.Form>
<!-- /form1 -->
<!-- form2 -->
<com.applib.libverify.Form
ohos:id="$+id:form2"
ohos:orientation="vertical"
ohos:width="match_parent"
ohos:height="match_content"
ohos:below="$+id:form1"
ohos:padding="16fp">
<!-- phone number -->
<com.applib.libverify.InputValidator
ohos:id="$+id:tiv1"
ohos:width="match_content"
ohos:height="match_content"
app:required="true">
<TextField
ohos:width="match_parent"
ohos:height="match_content"
ohos:text_input_type="pattern_number"
ohos:text_size="20fp"
ohos:hint="Phone number *"/>
</com.applib.libverify.InputValidator>
<!-- age -->
<com.applib.libverify.InputValidator
ohos:width="match_content"
ohos:height="match_content"
app:minValue="12">
<TextField
ohos:width="match_parent"
ohos:height="match_content"
ohos:text_input_type="pattern_number"
ohos:text_size="20fp"
ohos:hint="Age (12 min)" />
</com.applib.libverify.InputValidator>
<!-- submit form -->
<Button
ohos:id="$+id:validate_form2"
ohos:width="match_content"
ohos:height="match_content"
ohos:text_size="20fp"
ohos:text="Validate form2"/>
</com.applib.libverify.Form>
<!-- /form2 -->// get the forms
// you don't have to instantiate them because they already know the fields they have to validate
Form form1 = (Form) findComponentById(ResourceTable.Id_form1);
Form form2 = (Form) findComponentById(ResourceTable.Id_form2);
// validate form1
if(form1.isValid()) {
// form1 is valid
}
// validate form2
if(form2.isValid()) {
// form2 is valid
}It is recommended to use the builders to create Form and InputValidator views programmatically in order to prevent some errors. All the attributes for the two views are supported by the builders.
InputValidator inputValidator = new InputValidator.Builder(this)
// methods
.build();| Method | Return value | Description |
|---|---|---|
InputValidator.Builder(Context context) |
InputValidator.Builder |
The constructor of the builder |
on(TextField textField) |
InputValidator.Builder |
The TextField to validate |
required(boolean required) |
InputValidator.Builder |
Whether the field is required or not |
validatorType(int type) |
InputValidator.Builder |
Use a validator type predefined by FormValidator. You can use InputValidator.IS_EMAIL, InputValidator.IS_PHONE_NUMBER, InputValidator.IS_NUMERIC, InputValidator.IS_URL or InputValidator.IS_IP |
customValidator(AbstractValidator validator) |
InputValidator.Builder |
Use a custom validator |
minLength(int length) |
InputValidator.Builder |
The minimum length of the field |
maxLength(int length) |
InputValidator.Builder |
The maximum length of the field |
minValue(int value) |
InputValidator.Builder |
The minimum value of the field (must be numeric) |
maxValue(int value) |
InputValidator.Builder |
The maximum value of the field (must be numeric) |
regex(String regex) |
InputValidator.Builder |
Use a regex to validate a field |
identicalAs(int id) |
InputValidator.Builder |
The id of an TextField to which the field must be equal |
identicalAs(TextField textField |
InputValidator.Builder |
An other TextField to which the field must be equal |
errorMessage(String message) |
InputValidator.Builder |
The message to display if the field is not valid |
requiredMessage(String message) |
InputValidator.Builder |
The message to display if the field is empty but was required |
build() |
InputValidator |
Create the InputValidator object |
Form form = new Form.Builder(this)
// methods
.build();| Method | Return value | Description |
|---|---|---|
Form.Builder**(Context context, Component rootView) |
Form.Builder |
Constructor of the builder |
addInputValidator(InputValidator validator) |
Form.Builder |
Add an InputValidator |
showErrors(boolean show) |
Form.Builder |
Whether the errors must be shown on each TextField or not |
build() |
Form |
Create the Form object |
A sample app with some use cases of the library is available on this link