More powerful Validation from Respect
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.
Leave a comment
Use the form below to leave a comment:
Responses and Pingbacks
March 9th, 2011 at 7:49 am
In the line
$result = v::numeric()->between(1, 10)validate($value);
you mean
$result = v::numeric()->between(1, 10)->validate($value);
didn’t you?
March 9th, 2011 at 3:49 pm
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!
March 9th, 2011 at 4:13 pm
Thanks for the 2 corrections, updated the post.
May 16th, 2011 at 2:09 am
[…] More powerful Validation from Respect « php|architect – The site for PHP professionals. […]
July 19th, 2011 at 2:45 am
Can’t wait to try this out! I really like that it’s namespaced.
September 6th, 2011 at 9:45 am
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.
June 2nd, 2016 at 8:37 am
[…] está nos principais veículos de mídias especializadas do brasil e do exterior como no SitePoint, PHPArch, Zurb, iMasters entre muitos […]
June 3rd, 2016 at 1:51 am
[…] veículos de mídias especializadas do brasil e do exterior como no SitePoint, PHPArch, Zurb, iMasters entre muitos […]