More powerful Validation from Respect

Posted by on March 9, 2011
 

Dirty data: nobody likes it. It’s one of the biggest problems any web developer faces, where the input given by a user must validated before it can be accepted. Without this necessary step someone could wreck all sorts of havoc on your application, whether it’s just causes errors to appear or worst case opens security holes to allow malicious activity.

Lately I’ve been looking into libraries that can help solve this problem with ease, and I ran across one written by the team that goes by the name Respect. It’s simply called Validation, and it is a very simple and straightforward PHP 5.3 based validation library. The goal for them is to make validation easy and painless, using clever constructs like chaining and enabling developers to create reuseable components.

Let’s take a look at a simple example where we want to validate the given value is numeric.

use Respect\Validation\Validator as v;
// $result is true if $value is numeric
$result = v::numeric()->validate($value);

See how simple this is? I think the chain-ability of the validator methods is what really makes the library powerful in my eyes. So let’s say not only must this be numeric, but a number between 1 and 10. Chaining makes this easy.

use Respect\Validation\Validator as v;
// $result is true if $value is numeric and between 1 and 10
$result = v::numeric()->between(1, 10)->validate($value);

And you can do other validation other than numeric validation. Like for example, let’s say you want to make sure the input is an alpha only string, between 10 and 15 characters long, and has no whitespace characters in it. Behold!

use Respect\Validation\Validator as v;
// $result is true if $value is alpha, between 10 and 15
// characters long, and no whitespace
$result = v::alpha()->length(10, 15)->noWhitespace()->validate($value);

And not all validation rules need to be of the “AND” variety; Respect also allows the developer to write composite rules that are an “OR” condition. For example, let’s say you’ll accept an number between 1 and 10 or the string “NO”. This is the rule you can use to make that happen:

use Respect\Validation\Validator as v;
// $result is true if $value is alpha, between 10 and 15
// characters long, and no whitespace
$result = v::oneOf(
v::numeric()->between(1, 10),
v::alpha()->equals('NO')
)->validate($value);

This is just the beginning of the types of validation rules you can build with Validation. The development of the library is quite early on, with no official release yet, but you can grab the latest source code at their GitHub repository.


About the author—John Mertic is a senior software engineer and serves as the partner and community developer liaison at SugarCRM, having several years of experience with PHP web applications. At SugarCRM, he has specialized in data integration, mobile and user interface architecture. An avid writer, he has been published in php|architect, IBM Developerworks, and in the Apple Developer Connection, and is the author of the book 'The Definitive Guide to SugarCRM: Better Business Applications'. He has also contributed to many open source projects, most notably the PHP project where is the creator and maintainer of the PHP Windows Installer.