ValiFi


Source link: https://github.com/mlykotom/valifi

ValiFi

  • Are you tired of creating forms in app and checking all the possible inputs?
  • Would you like to validate things in realtime instead of after submit?
  • Are you using data binding or would like to try?

ValiFi is for you!

  • ValiFi is android library for validating fields or whole forms.
  • It's working with data binding and validations are visible immediately when user adds input.
  • It's highly customizable and simple for use.

How to use

Initialize the library

1. Add gradle dependency

compile 'com.mlykotom:valifi:1.1.6'

2. Setup project with data binding

android{

  ...
  dataBinding {

enabled = true  
}

  ... 
}

3. Install the library in application class

public class MyApplication extends Application {

  @Override
  public void onCreate() {

ValiFi.install(this);

  
}
 
}

Use in your code

0. Bind your ViewModel/Fragment/etc to layout (you may already have this)

You may refer to Android's reference for data binding: https://developer.android.com/topic/libraries/data-binding/index.html

(examples uses inloop's library https://github.com/inloop/AndroidViewModel)

1. Create field you want to validate

public final ValiFieldEmail email = new ValiFieldEmail();

2. Use it from layout

Library uses two-way data binding so be careful of adding android:text=" @={ ... } "

<android.support.design.widget.TextInputLayout
  ...
  app:errorEnabled="true"
  app:error="@{
viewModel.email.error
}
">

<android.support.design.widget.TextInputEditText

...

 android:text="@={
viewModel.email.value
}
"

android:hint="E-mail address" /> </android.support.design.widget.TextInputLayout>  ...  <Button
  ...
  android:enabled="@{
viewModel.email.isValid
}
"
  android:text="Submit" />

3. Destroy the field

In order to prevent leaks, field must be destroyed before destroying the screen!

This is easily done by calling:

@Override public void onDestroy() {
  email.destroy();
  super.onDestroy();
 
}

Or if you have more than one field:

@Override public void onDestroy() {
  ValiFi.destroyFields(email, password);
  super.onDestroy();
 
}

Or if you use form (see Forms!):

@Override public void onDestroy() {
  form.destroy();
  super.onDestroy();
 
}

And That's it!

When user types his e-mail, it will automatically validates input and enables/disables submit button.

Customizations

There are plenty of options to customizate ValiFi.

Globally (for whole app)

public class MyApplication extends Application {

  @Override
  public void onCreate() {
  ValiFi.install(this,
 new ValiFi.Builder()
 .setErrorDelay(1000) // possibility of .setErrorDelay(ValiFiErrorDelay.NEVER)
 .setErrorResource(ValiFi.Builder.ERROR_RES_EMAIL, R.string.my_custom_email_error)
 .setPattern(ValiFi.Builder.PATTERN_EMAIL, Patterns.EMAIL_ADDRESS)
 .build()  );

  
}
 
}

Locally (for specified field)

public final ValiFieldEmail email = new ValiFieldEmail("default nullable value", "Custom error message");

Specify your field by adding validators

public final ValiFieldText fieldWithDifferentValidations = new ValiFieldText();
  fieldWithDifferentValidations  .addRangeLengthValidator(3, 10)  .setEmptyAllowed(true)  .addPatternValidator("pattern not valid", Patterns.IP_ADDRESS)  .addCustomValidator("custom not valid", new ValiFieldBase.PropertyValidator<String>() {

@Override
public boolean isValid(@Nullable String value) {

 return whenThisIsValid;

}
  
}
);
 

Forms!

Want to allow submit after all fields valid?

public final ValiFieldEmail email = new ValiFieldEmail();
 public final ValiFieldPassword password = new ValiFieldPassword();
  public final ValiFiForm form = new ValiFiForm(email, password);
<Button  ...  android:enabled="@{
viewModel.form.isValid
}
"  ...  android:text="Submit" />

For more information about customizing visit Wiki

Examples

  1. MVVM approach ( preferred) here
  2. Classic fragment approach here

License

Copyright 2017 Tomas Mlynaric  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

multiline-collapsingtoolbar is a replacement for CollapsingToolbarLayout from the Android Design Support Library which can deal with multiline titles in the expanded state. When collapsing the toolbar, the lower lines of the title fade away to leave only the top line visible.

Joins operators for RxJava.

An useful fragment navigator helps you control fragments better and easier. You will not need to worry about the dirty things like switch fragments, fragments overlay, save fragments states and restore fragments states. The lib will do them all for you. Fragments in activity and nest Fragment are all supported.

OkHttp mockwebserver with fixtures extension.

It is a custom view implementation to showcase title, image, and description by the given URL link.

A powerful and simple library to open issues on GitHub directly from your app.

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