php[architect] logo

Want to check out an issue? Sign up to receive a special offer.

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.


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.
Tags: , , ,
 

Responses and Pingbacks

In the line


$result = v::numeric()->between(1, 10)validate($value);

you mean

$result = v::numeric()->between(1, 10)->validate($value);

didn’t you?

Wow, how nice is to have a post about my project on php|arch? =D

I believe the last sample should be something like this:

$result = v::oneOf(
v::numeric()->between(1, 10),
v::alpha()->equals(‘NO’)
)->validate($value);

Thank you for the post!

Thanks for the 2 corrections, updated the post.

[…] More powerful Validation from Respect « php|architect – The site for PHP professionals. […]

Wayne Fincher on
July 19th, 2011 at 2:45 am

Can’t wait to try this out! I really like that it’s namespaced.

Awesome use of latest php 5 namespaces and chanability.
One thing i’d mention for the author is the order in the chain is more natural to have the value first then apply validators.
Instead of $result = v::numeric()->between(1, 10)->validate($value); i believe $result = v::validate($value)->numeric()->between(1, 10); would be easier to learn and read.

[…] está nos principais veículos de mídias especializadas do brasil e do exterior como no SitePoint, PHPArch, Zurb, iMasters entre muitos […]

[…] veículos de mídias especializadas do brasil e do exterior como no SitePoint, PHPArch, Zurb, iMasters entre muitos […]

Leave a comment

Use the form below to leave a comment: