Live by the rules baby! At least a few ;)

I have personally detested writing boilerplate code. And this has urged me to write libraries for Android like the Simple Section Adapter and a few others in the past. This time, it’s the validation that has caused me distress during development. Why does validation have to be painful? This question resulted in the birth of a new validation library for Android.

Here in this blog, I introduce you Android Saripaar – A rule-based validation library.

Thinking Validation (existing scenario)

Let’s take an example, I have a user registration screen with a three EditText widgets and a Button.

  1. Username (EditText)
  2. Password (EditText)
  3. Confirm Password (EditText)
  4. Register (Button)

Now clicking on the “Register” button has to do the following:

  1. Check if the Username is available (i.e. unique).
  2. Check if the text in the Password and the Confirm Password are equal.
  3. If 1 and 2 succeeds, then register a new user account.
  4. If 1 or 2 fails, then notify the user with appropriate feedback (Dialog / Toast / Animation)

Current Approach

One of the straightforward approaches to solving the above-mentioned problem would be to write an AsyncTask that would make a request to the remote server and check if the Username is unique and in theAsyncTask.postExecute(Result) callback, check if the Password and Confirm Password fields are equal and if all goes well, send another request to the remote server and create a new user account.

Short Comings

  1. Too much intrusive, tangled (this is one of the important reasons for authoring Saripaar) and boilerplate code.
  2. Design for asynchronous validation (When situation calls for it).
  3. Aesthetics, why not put all validation constraints in one place?

Saripaar Advantages

  1. Rule-based – Saripaar is full of rules, whenever you want to validate a field, you write a rule. The library comes with a set of rules that you can use out of the box. In case you have your own situation or a component to take care of, you can simply extend the Rule class and write your own rule.
  2. Synchronous is Asynchronous – When you have to do an asynchronous validation like a network operation or querying your magnanimous SQLite database you don’t have to think of threading / AsyncTask, Saripaar does it all for you.
  3. Callbacks – Receive callbacks if your validation is successful, when a rule fails or even if your validation is cancelled.

Code

No programming article is complete without an example. Following is a glimpse of Saripaar’s usage. You can find the project on GitHub, there is no user guide as of now, however, the source code is completely documented and is at your disposal. The following snippet should give you an idea of its ease of use. I have planned a wiki for the project. So when that is done, it will be a comprehensive guide for beginners.

// Validator & listener
validator = new Validator();
validator.setOnValidationListener(this);

// Create a new rule
Rule usernameRule = new Rule<EditTex>("Username is taken :(") {

    @Override
    public boolean isValid<EditText>(EditText editText) {
        // All your synchronous HTTP request, parsing, etc.,
        return isUnique;
    }
}

validator.put(userNameEditText, uniqueUsernameRule);
validator.put(confirmPwdEditText,
        Rules.eq("Passwords don\'t match", pwdEditText));

To begin validation, call the Validator.validateAsync() method from your event handler, usually the View.OnClickListener and handle the appropriate callbacks – preValidation(), onSuccess(), onFailure(View failedView, Rule failedRule) and onValidationCancelled().Feel free to post your queries as comments, I’ll be happy to answer.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s