Valitron is a simple, minimal and elegant stand-alone PHP validation library with NO dependencies. Valitron uses simple, straightforward validation methods with a focus on readable and concise syntax.
Why Another Validation Library?
Valitron was created out of frustration with other validation libraries that have dependencies on large components from other frameworks unrelated to validation like Illuminate Validation (laravel 4) requiring Symfony HttpFoundation, pulling in a ton of extra files that aren’t needed for basic validation. It also has purposefully simple syntax used to run all validations in one call instead of individually validating each value by instantiating new classes and validating values one at a time like Respect Validation requires you to do. Valitron also has a focus on being concise – validation rules are just a single line per rule, and can include multiple fields in an array. This is handy, because in most use cases, a single validation rule – like “required” will be applied to many fields, so it doesn’t make sense to start with the field first like Fuel Validation does.
In short, Valitron is everything you’ve been looking for in a validation library but haven’t been able to find until now: simple pragmatic syntax, lightweight code that makes sense, extensibility for custom callbacks and validations, good test coverage, and no dependencies.
Usage Example
Valitron is made to setup all your validation rules on the fields you need, and then run all the validations in one call. This is better than validating the fields one-by-one, because that approach causes a lot of “if” statements and branching logic that doesn’t make the resulting code any better than doing the validations by hand (which obviously sucks).
1 2 3 4 5 6 7 8 9 10 11 | $v = new Valitron\Validator($_POST); // Input array $v->rule('required', ['name', 'email', 'date_start']); $v->rule('email', 'email'); // Email uses filter_var $v->rule('dateAfter', 'date_start', new \DateTime()); // After today if($v->validate()) { echo "Yay! We're all good!"; print_r($v->data()); } else { // Errors print_r($v->errors()); } |
More usage examples and documentation can be found on the Valitron GitHub Page. Valitron is on Packagist, and can be installed via Composer.



Jeez, man, you could have used Aura.Filter if you wanted something without dependencies.
https://github.com/auraphp/Aura.Filter
Aura.Filter looks nice, though slightly more verbose than Valitron. It didn’t come up in my searches for “validation” on Packagist otherwise I may have just used it instead of making this. Regardless, here we are.
Respect validation does not require you to run validation one at a time.
For example you can run validations like this: v::key(‘email’, v::email())->key(‘name’, v::alnum()->noWhitespace()->length(1, 15))->assert($_POST)
Hey Vance.
I’m the author of Respect\Validation. What did you mean by “validating values one at a time like Respect Validation requires you to do”?
Your sample can be rewritten in Respect\Validation as:
try {
v::attribute(‘name’)
->attribute(‘email’, v::email()))
->attribute(‘date_start’, v::date()->min(‘today’))
->assert($_POST);
echo “Yay! We’re all good!”;
} catch (Exception $e) {
print_r($e->findMessages(array(‘name’, ‘email’, ‘date_start’));
}
Thank you!
Alexandre,
Sorry if I am mis-representing your library here. None of the examples I looked at on your project’s GitHub page showed a usage example for validating multiple fields at once – they were all one at a time, which the library seems to be geared for. I will update my post with better wording.
API looks very similar to validation in Yii framework.
I just checked out Yii validation, and I really don’t see the resemblance. The syntax is different and it’s tied to the Model. It’s also not an independent component that is useable outside the Yii framework, so I think the comparison is a little flawed.
…when did PHP get [...] array constructors? It’s trying harder to be Perl all the time.
I’ve tried all the validation libraries you mentioned, and I agree they all have their problems. Respect makes it a pain to get out error messages in any sane format, and Symfony returns errors in a zero indexed array, making it difficult to find the error for a particular field. Just setting up Laravel’s validator standalone is insane too, dependency after dependency needs to be injected just to get a working validator.
The only problem I can find with Valitron so far is that the error message customisation needs some work. It would be nice if I could label my fields and use them in the error messages. If it could do that it would be a huge improvement.