AndroidVerify


Source link: https://github.com/pchmn/AndroidVerify

AndroidVerify

Android library designed for rapid and customizable form validation.

Demo

Download sample-v1.0.2.apk

Setup

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

In your project level build.gradle :

allprojects {

  repositories {

...

maven {
 url "https://jitpack.io" 
}

  
}
 
}

 

In your app level build.gradle :

dependencies {

  compile 'com.github.pchmn:AndroidVerify:1.0.2' 
}

Usage

You can use AndroidVerify with any View that extends the original EditText (such as MaterialEditText for example).

With XML

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

<com.pchmn.androidverify.InputValidator
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  app:required="true"
  app:requiredMessage="Email required">

<EditText

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:inputType="textEmailAddress"

android:hint="Email"/>  </com.pchmn.androidverify.InputValidator>

  <com.pchmn.androidverify.InputValidator
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  app:regex="^[0-9]{
4
}
$"
  app:errorMessage="4 digits only">

<EditText

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:hint="Regex 4 digits (custom error msg)"/>  </com.pchmn.androidverify.InputValidator>

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 :

// 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) :

// 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 :

// 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. 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

 <!-- form1 -->
  <com.pchmn.androidverify.Form

android:id="@+id/form1"

android:orientation="vertical"

android:layout_width="match_parent"

android:layout_height="wrap_content"

app:showErrors="false">

 <!-- email -->

<com.pchmn.androidverify.InputValidator

 android:layout_width="wrap_content"

 android:layout_height="wrap_content">

  <EditText

  android:layout_width="match_parent"

  android:layout_height="wrap_content"

  android:inputType="textEmailAddress"

  android:hint="Email"/>

 </com.pchmn.androidverify.InputValidator>

 <!-- password -->

<com.pchmn.androidverify.InputValidator

 android:layout_width="wrap_content"

 android:layout_height="wrap_content"

 app:required="true"

 app:minLength="6">

  <EditText

  android:layout_width="match_parent"

  android:layout_height="wrap_content"

  android:inputType="textPassword"

  android:hint="Password (6 char. min) *" />

 </com.pchmn.androidverify.InputValidator>

</com.pchmn.androidverify.Form>
  <!-- /form1 -->

<!-- form2 -->
  <com.pchmn.androidverify.Form

android:id="@+id/form2"

android:orientation="vertical"

android:layout_width="match_parent"

android:layout_height="wrap_content">

 <!-- phone number -->

<com.pchmn.androidverify.InputValidator

 android:layout_width="wrap_content"

 android:layout_height="wrap_content"

 app:required="true">

  <EditText

  android:layout_width="match_parent"

  android:layout_height="wrap_content"

  android:inputType="phone"

  android:hint="Phone number *"/>

 </com.pchmn.androidverify.InputValidator>

 <!-- age -->

<com.pchmn.androidverify.InputValidator

 android:layout_width="wrap_content"

 android:layout_height="wrap_content"

 app:minValue="12">

  <EditText

  android:layout_width="match_parent"

  android:layout_height="wrap_content"

  android:inputType="number"

  android:hint="Age (12 min)" />

 </com.pchmn.androidverify.InputValidator>

</com.pchmn.androidverify.Form>
  <!-- /form2 -->

Validate the forms

// 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

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

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

Credits

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. 

Resources

It is a RecyclerView using GridLayoutManager with header layout as ViewPager. Project could be easily customized according to the usages.

Resizable rectangle overlay.

This library lets you to use Dropbox HTTP API v2.

OPFMaps is an open source library which provides an easy way for developers to integrate different android maps into their apps.

Android Game Boy Emulator written in Java.

Autogenerated java hamcrest matchers for pojo with help of AnnotationProcessor.

Topics


2D Engines   3D Engines   9-Patch   Action Bars   Activities   ADB   Advertisements   Analytics   Animations   ANR   AOP   API   APK   APT   Architecture   Audio   Autocomplete   Background Processing   Backward Compatibility   Badges   Bar Codes   Benchmarking   Bitmaps   Bluetooth   Blur Effects   Bread Crumbs   BRMS   Browser Extensions   Build Systems   Bundles   Buttons   Caching   Camera   Canvas   Cards   Carousels   Changelog   Checkboxes   Cloud Storages   Color Analysis   Color Pickers   Colors   Comet/Push   Compass Sensors   Conferences   Content Providers   Continuous Integration   Crash Reports   Credit Cards   Credits   CSV   Curl/Flip   Data Binding   Data Generators   Data Structures   Database   Database Browsers   Date &   Debugging   Decompilers   Deep Links   Dependency Injections   Design   Design Patterns   Dex   Dialogs   Distributed Computing   Distribution Platforms   Download Managers   Drawables   Emoji   Emulators   EPUB   Equalizers &   Event Buses   Exception Handling   Face Recognition   Feedback &   File System   File/Directory   Fingerprint   Floating Action   Fonts   Forms   Fragments   FRP   FSM   Functional Programming   Gamepads   Games   Geocaching   Gestures   GIF   Glow Pad   Gradle Plugins   Graphics   Grid Views   Highlighting   HTML   HTTP Mocking   Icons   IDE   IDE Plugins   Image Croppers   Image Loaders   Image Pickers   Image Processing   Image Views   Instrumentation   Intents   Job Schedulers   JSON   Keyboard   Kotlin   Layouts   Library Demos   List View   List Views   Localization   Location   Lock Patterns   Logcat   Logging   Mails   Maps   Markdown   Mathematics   Maven Plugins   MBaaS   Media   Menus   Messaging   MIME   Mobile Web   Native Image   Navigation   NDK   Networking   NFC   NoSQL   Number Pickers   OAuth   Object Mocking   OCR Engines   OpenGL   ORM   Other Pickers   Parallax List   Parcelables   Particle Systems   Password Inputs   PDF   Permissions   Physics Engines   Platforms   Plugin Frameworks   Preferences   Progress Indicators   ProGuard   Properties   Protocol Buffer   Pull To   Purchases   Push/Pull   QR Codes   Quick Return   Radio Buttons   Range Bars   Ratings   Recycler Views   Resources   REST   Ripple Effects   RSS   Screenshots   Scripting   Scroll Views   SDK   Search Inputs   Security   Sensors   Services   Showcase Views   Signatures   Sliding Panels   Snackbars   SOAP   Social Networks   Spannable   Spinners   Splash Screens   SSH   Static Analysis   Status Bars   Styling   SVG   System   Tags   Task Managers   TDD &   Template Engines   Testing   Testing Tools   Text Formatting   Text Views   Text Watchers   Text-to   Toasts   Toolkits For   Tools   Tooltips   Trainings   TV   Twitter   Updaters   USB   User Stories   Utils   Validation   Video   View Adapters   View Pagers   Views   Watch Face   Wearable Data   Wearables   Weather   Web Tools   Web Views   WebRTC   WebSockets   Wheel Widgets   Wi-Fi   Widgets   Windows   Wizards   XML   XMPP   YAML   ZIP Codes