An open API service indexing awesome lists of open source software.

https://github.com/pchmn/androidverify

Android library designed for rapid and customizable form validation.
https://github.com/pchmn/androidverify

android-library easy-to-use form-validation java

Last synced: 7 months ago
JSON representation

Android library designed for rapid and customizable form validation.

Awesome Lists containing this project

README

          

# AndroidVerify

Android library designed for rapid and customizable form validation.

[![Release](https://jitpack.io/v/pchmn/AndroidVerify.svg)](https://jitpack.io/#pchmn/AndroidVerify)

## Demo
[Download sample-v1.0.2.apk](https://github.com/pchmn/AndroidVerify/raw/master/docs/android-verify-v1.0.2.apk)

## Setup

To use this library your `minSdkVersion` must be >= 15.

In your project level build.gradle :
```java
allprojects {
repositories {
...
maven { url "https://jitpack.io" }
}
}
```

In your app level build.gradle :
```java
dependencies {
compile 'com.github.pchmn:AndroidVerify:1.0.2'
}
```

## Usage

You can use **AndroidVerify** with any `View` that extends the original [`EditText`](https://developer.android.com/reference/android/widget/EditText.html) (such as
[`MaterialEditText`](https://github.com/rengwuxian/MaterialEditText) for example).

### With XML

You just have to wrap your `EditText` with an `InputValidator` view. Example for an email and a custom regex :

```xml


```
`InputValidator` will automatically recognized **textEmailAdress**, **phone** and **number** `inputType` and use the appropriate validator, like in the example with the email field.

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 from an activity
// 'this' represents an Activity
// you can specify if you want to show error messages or not
Form form = new Form.Builder(this)
.showErrors(true)
.build();

// or initiate from a fragment or from what you want by providing your own root view
Form form = new Form.Builder(getContext(), rootView)
.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
// emailEditText is the EditText to validate
// 'this' represents a Context
InputValidator emailValidator = new InputValidator.Builder(this)
.on(emailEditText)
.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 the `InputValidator` view. 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 EditText 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

#### `Form`
All the attributes that can be used with the `Form` view. 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 EditText 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 inputValidator = (InputValidator) findViewById(R.id.input_validator);
// your custom validator must extends AbstractValidator class
inputValidator.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(anEditText)
.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` view in XML

If you want, you can use a `Form` view directly in XML. This view extends [`LinearLayout`](https://developer.android.com/reference/android/widget/LinearLayout.html). 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









```

#### Validate the forms

```java
// get the forms
// you don't have to instantiate them because they already know the fields they have to validate
Form form1 = (Form) findViewById(R.id.form1);
Form form2 = (Form) findViewById(R.id.form2);

// validate form1
if(form1.isValid()) {
// form1 is valid
}

// validate form2
if(form2.isValid()) {
// form2 is valid
}

```

### Builders method

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` builder

```java
InputValidator inputValidator = new InputValidator.Builder(this)
// methods
.build();
```

Method | Return value | Description
--- | --- | ---
`InputValidator.Builder(Context context)` | `InputValidator.Builder` | The constructor of the builder
`on(EditText editText)` | `InputValidator.Builder` | The EditText 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 EditText to which the field must be equal
`identicalAs`(EditText editText)` | `InputValidator.Builder` | An other EditText 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` builder

```java
Form form = new Form.Builder(this)
// methods
.build();
```

Method | Return value | Description
--- | --- | ---
`Form.Builder(Activity activity)` | `Form.Builder` | First constructor of the builder
`Form.Builder**(Context context, View rootView)` | `Form.Builder` | Second constructor of the builder
`Form.Builder(Context context)` | `Form.Builder` | Third constructor of the builder. Be aware of possibly inflating errors using this constructor
`addInputValidator(InputValidator validator)` | `Form.Builder` | Add an `InputValidator`
`showErrors(boolean show)` | `Form.Builder` | Whether the errors must be shown on each EditText or not
`build()` | `Form` | Create the `Form` object

## Sample

A sample app with some use cases of the library is available on this [link](https://github.com/pchmn/FormValidator/tree/master/sample)

## Credits

* [Ratifier](https://github.com/hamadakram/ratifier?utm_source=android-arsenal.com&utm_medium=referral&utm_campaign=5441)
* [Android-Validator](https://github.com/throrin19/Android-Validator)

## License

```
Copyright 2017 pchmn

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
```