GithubHelp home page GithubHelp logo

android-saripaar's Introduction

Android Saripaar

சரிபார் - sari-paar (Tamil for "to check", "verify" or "validate")

Android Saripaar is a simple, yet powerful rule-based UI validation library for Android. It is the SIMPLEST validation library available for Android.

Why Android Saripaar?

  • Declarative style validation powered by Annotations.
  • Does both Synchronous and Asynchronous validations, you don't have to worry about threading.
  • Works with Stock Android Widgets, no custom view dependencies.
  • Quick to setup, just download the jar and include it in your libs project folder.
  • Takes most of your validation logic out of your code.
  • Compatible with other annotation frameworks such as AndroidAnnotations, RoboGuice, etc.,

Quick Start

Step 1 - Annotate your widgets using Saripaar Annotations

@Required(order = 1)
@Email(order = 2)
private EditText emailEditText;

@Password(order = 3)
@TextRule(order = 4, minLength = 6, message = "Enter at least 6 characters.")
private EditText passwordEditText;

@ConfirmPassword(order = 5)
private EditText confirmPasswordEditText;

@Checked(order = 6, message = "You must agree to the terms.")
private CheckBox iAgreeCheckBox;

The annotations are self-explanatory. The order attribute is mandatory and specifies the order in which the validations will be performed by the library.

Step 2 - Instantiate a new Validator

public void onCreate() {
    super.onCreate();
    // Code…

    validator = new Validator(this);
    validator.setValidationListener(this);

    // More code…
}

You will need a Validator for the current Activity and also a ValidationListener for callbacks on validation events.

Step 3 - Implement a ValidationListener

public class RegistrationActivity implements ValidationListener {

    public void onSuccess() {
        // Create a new account…
        Toast.makeText(this, "Yay! we got it right!", Toast.LENGTH_SHORT).show();
    }

    public void onFailure(View failedView, Rule<?> failedRule) {
        String message = failedRule.getFailureMessage();

        if (failedView instanceof EditText) {
            failedView.requestFocus();
            failedView.setError(message);
        } else {
            Toast.makeText(this, message, Toast.LENGTH_SHORT).show();
        }
    }

    public void preValidation() {
        // Do nothing…
    }

    public void onValidationCancelled() {
        // Do nothing
    }
}
  • onSuccess() - Called when all your views pass all validations.
  • onFailure(View, Rule<?>) - Called when a Rule fails, you receive the View along with the Rule that failed.
  • preValidation() - Called before the validation starts, useful during asynchronous validations.
  • onValidationCancelled() - Called when an asynchronous validation is cancelled, never called during synchronous validations.

Step 4 - Perform validation

registerButton.setOnClickListener(new OnClickListener() {
    public void onClick(View v) {
        validator.validate();
    }
});

The Validator.validate() call runs the validations and returns the result via appropriate callbacks on the ValidationListener. You can run validations on a background AsyncTask by calling the Validator.validateAsync() method. You can call both the methods from any event listener such as the OnClickListener, TextWatcher, OnFocusChangedListener, OnTouchListener, etc.,

Dependencies

  • None

Please visit the wiki for a complete guide on Android Saripaar.

License

Copyright 2012 Mobs and Geeks

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.

android-saripaar's People

Contributors

ragunathjawahar avatar tbruyelle avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.