data-binding-validator


Source link: https://github.com/Ilhasoft/data-binding-validator

Data Binding Validator by Ilhasoft

The Data Binding Validator makes it easy and quick to validate fields in forms using data binding framework.

Download

Step 1: Add it in your root build.gradle at the end of repositories:

allprojects {

repositories {

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

}
 
}
 

Step 2: Add the dependency

  dependencies {

  compile 'com.github.Ilhasoft:data-binding-validator:LATEST-VERSION'

}
 

Latest Version:

Features:

  • Minimum/Maximum length validation for text fields;
  • Validate inputs based on field type (email, credit card, URL, CPF and so on);
  • Pre-defined error messages translated into English, Portuguese and Spanish;
  • Custom error messages by field;
  • Supports TextInputLayout and EditText;

Sample

Usage

Enabling Data Binding

You need to enable Data Binding to use this library, add the following code into your main module's build.gradle:

android {

  ....
  dataBinding {

enabled = true
  
}
 
}
 

Setting up validations directly on layout

It's possible to insert directly on layout creation, the validation on input fields. The error messages in different languages already are configured inside the library, not requiring the adding by developers. These are the existing validation types:

Validate Characters Length

Adding validateMinLength or validateMaxLength to your EditText, it's possible to configure a minimum or maximum characters length:

<EditText
android:id="@+id/name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Name"
app:validateMinLength="@{
4
}
"
app:validateMaxLength="@{
10
}
"/> 

Validate Empty Characters

Adding validateEmpty, you can validate if the EditText is empty:

<EditText
android:id="@+id/hello"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Name"
app:validateEmpty="@{
true
}
" /> 

Validate Date Patterns

Adding validateDate, you can set a pattern accepted by the EditText such as dd/MM/yyyy, yyyy and so on:

<EditText
android:id="@+id/date"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Name"
app:validateDate='@{
"dd/MM/yyyy"
}
' /> 

Validate Regex

Adding validateRegex, you can set a regular expression to be validated, for example:

<EditText
android:id="@+id/regex"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Name"
app:validateRegex='@{
"[a-zA-Z0-9-._]+"
}
'
app:validateRegexMessage="@{
@string/regexErrorMessage
}
" /> 

Validate Input Types

You can even validate input by date, for example Email, URL, Username, CreditCard, CPF, CEP and so on:

<EditText app:validateType='@{
"email"
}
' />  <EditText app:validateType='@{
"url"
}
' />  <EditText app:validateType='@{
"creditCard"
}
' />  <EditText app:validateType='@{
"username"
}
' />  <EditText app:validateType='@{
"cpf"
}
' /> 

Applying Validation

It will be necessary to instantiate Validator passing as argument your ViewDataBinding instance got from your layout binding. After that you can call validate() that will return if your data is valid or not. Example:

@Override protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

 MainActivityBinding binding = DataBindingUtil.setContentView(this, R.layout.main_activity);

final Validator validator = new Validator(binding);

 binding.validate.setOnClickListener(new View.OnClickListener() {

  @Override
  public void onClick(View v) {

 if (validator.validate()) {

saveToDatabase();

 
}

  
}

}
);
 
}
 

Or you can use toValidate() if prefer using listener to validation response:

public class YourActivity extends AppCompatActivity implements Validator.ValidationListener {

...

@Override
  protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

 MainActivityBinding binding = DataBindingUtil.setContentView(this, R.layout.main_activity);

final Validator validator = new Validator(binding);

validator.setValidationListener(this);

 binding.validate.setOnClickListener(new View.OnClickListener() {

 @Override

 public void onClick(View v) {

  validator.toValidate()

 
}

}
);

  
}

@Override
  public void onValidationSuccess() {

saveToDatabase();

  
}

@Override
  public void onValidationError() {

Toast.makeText(YourActivity.this, "Dados inválidos!", Toast.LENGTH_SHORT).show();

  
}
 
}
 

Custom Error Messages

You can add custom error messages by using the same validation rule name and adding Message at the end, such as validateTypeMessage, validateDateMessage, validateRegexMessage and so on. For example:

<EditText
android:id="@+id/date"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Name"
app:validateDate='@{
"dd/MM/yyyy"
}
'
app:validateDateMessage="@{
@string/dateErrorMessage
}
" /> 

Validating

If you want to validate all the fields, you can simply call validator.validate(), to validate specific views you can call validator.validate(view) or validator.validate(viewsList);

Validation modes

The validation can be applied in two way, field by field or the whole form at once. By default, it's configured field by field, however, you can call validator.enableFormValidationMode(); to enable the validation of the whole form.

If you want to come back to the default way, call validator.enableFieldValidationMode();

Auto dismiss

By default, the library prompts error messages and doens't dismiss the error automatically, however, you can add on your layout validation the same rule name by adding AutoDismiss at the end, which receives a boolean. In this case it could dismiss the error automatically. For example:

<EditText
android:id="@+id/date"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Name"
app:validateDate='@{
"dd/MM/yyyy"
}
'
app:validateDateMessage="@{
@string/dateErrorMessage
}
"
app:validateDateAutoDismiss="@{
true
}
" /> 

License

Copyright 2017-present Ilhasoft  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

Just a cool background view.

It is generally known that load an unoptimized Dex file at runtime in Android (especially in ART mode) would take a long time. When your App is using MultiDex or PluginFramework, You will find that this problem is hard to bear.

TurboDex was born to solve this problem, Like to opens the god mode for AndroidVM, after using TurboDex, no matter how much Dex file your need to load, it will be finished in a very short time.

Legend is a Hook framework for Android Development, it allows you to Hook Java methods without ROOT. Even more exciting is that it supports both Dalvik and Art environment!

Helps setup the tasks needed to generate javadocs for an Android library.

DSL Platform compatible JSON library for Java and Android.

Retrofit ships with support for OkHttp's RequestBody and ResponseBody types but the library is content-format agnostic. This modules contained herein are additional converters for JSON which uses FastJson.

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