# OhosVerify **Repository Path**: applibgroup/OhosVerify ## Basic Information - **Project Name**: OhosVerify - **Description**: Library designed for rapid and customizable form validation. - **Primary Language**: Java - **License**: Apache-2.0 - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 0 - **Forks**: 0 - **Created**: 2021-10-13 - **Last Updated**: 2022-01-03 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README [![Build](https://github.com/applibgroup/OhosVerify/actions/workflows/main.yml/badge.svg)](https://github.com/applibgroup/OhosVerify/actions/workflows/main.yml) [![Quality Gate Status](https://sonarcloud.io/api/project_badges/measure?project=applibgroup_OhosVerify&metric=alert_status)](https://sonarcloud.io/dashboard?id=applibgroup_OhosVerify) # OhosVerify Ohos library designed for rapid and customizable form validation. # Source The library is inspired from the mentioned Android Library: [AndroidVerify](https://github.com/pchmn/AndroidVerify/) (version 1.0.2) ## Features The library supports rapid and customizable form validation which can be integrated using Java Builder functions or wrapping text fields via XML. It supports validation for Numeric, Phone Number, Email, IP Address, URL, Regex, Identical fields, Minimum/Maximum/Range value, and Minimum/Maximum/Range character length. The validators can also be [customized](#advanced-usage) as required. ## Dependency 1. For using Verify module in sample app, include the source code and add the below dependencies in entry/build.gradle to generate hap/support.har. ``` dependencies { implementation project(':ohosverify') testCompile 'junit:junit:4.12' } ``` 2. For using Verify module in separate application using har file, add the har file in the entry/libs folder and add the dependencies in entry/build.gradle file. ``` dependencies { implementation fileTree(dir: 'libs', include: ['*.har']) testCompile 'junit:junit:4.12' } ``` 3. For using Verify from a remote repository in separate application, add the below dependencies in entry/build.gradle file. ``` dependencies { implementation 'dev.applibgroup:ohosverify:1.0.0' testCompile 'junit:junit:4.12' } ``` ## Usage You can use **OhosVerify** with any `Component` that extends the original [`TextField`]. ### With XML You just have to wrap your `TextField` with an `InputValidator` view. Example for an email and a custom regex : ```xml ``` **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 : ```java // 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 } ``` ### With Java You can create programmatically `InputValidator` without passing by XML ([see all Builder methods](#inputvalidator-builder)) : ```java // 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. ### Attributes #### `InputValidator` 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 * Has limitations. Use Java builder methods for identical validators to avoid any issues. #### `Form` 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 ## Advanced Usage ### Use a custom validator You can use a custom validator for an `InputValidator` : ```java // 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(); ``` ### Use the `Form` component in XML If you want, you can use a `Form` component directly in XML. This view extends [`DirectionalLayout`](https://developer.harmonyos.com/en/docs/documentation/doc-references/directionallayout-0000001054238715). It must wrap all the fields you want to check. It can be useful for these reasons : * You don't have to instantiate a `Form` object 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 #### XML ```xml