PHP Valitron tutorial shows how to validate PHP values with Valitron validation package.
last modified February 16, 2025
PHP Valitron tutorial shows how to validate PHP values with Valitron validation package.
Valitron is a simple, minimal and elegant stand-alone validation library with no dependencies.
$ composer require vlucas/valitron $ composer require tightenco/collect
We install the Valitron package and the Laravel’s collection package.
In the first example, we show how to do a very simple validation.
simple.php
<?php
require(‘vendor/autoload.php’);
use Valitron\Validator;
$validator = new Validator([’name’ => ‘John Doe’]); $validator->rule(‘required’, ’name’);
if($validator->validate()) { echo “Validation passed”; } else {
$coll = collect($validator->errors());
$messages = $coll->flatten();
foreach ($messages as $message) {
echo $message . "\n";
}
}
The example validates one required value.
use Valitron\Validator;
We include the validator.
$validator = new Validator([’name’ => ‘John Doe’]);
We create the instance of the Validator and pass it a value to be validated.
$validator->rule(‘required’, ’name’);
We specify a required rule with the rule method.
if($validator->validate()) {
The validation is performed with validate.
$coll = collect($validator->errors());
$messages = $coll->flatten();
foreach ($messages as $message) { echo $message . “\n”; }
If the validation fails, we get the errors and display them.
$ php simple.php Validation passed
Valitron contains a set of predefined rules, such as required, email, min, max, or url.
The rules can be combined with the | character.
multiple_rules.php
<?php
require(‘vendor/autoload.php’);
use Valitron\Validator;
$rules = [ ‘required’ => [’name’, ’email’], ‘alphaNum’ => ’name’, ‘integer’ => ‘age’, ‘min’ => [[‘age’, 1]], ’email’ => ’email’ ];
$validator = new Validator([’name’ => ‘John Doe’, ‘age’ => 34]); $validator->rules($rules);
if ($validator->validate()) { echo “Validation passed”; } else { $errors = $validator->errors();
foreach ($errors as $arr) {
foreach ($arr as $error) {
echo $error . "\n";
}
};
}
The example uses several validation rules.
$rules = [ ‘required’ => [’name’, ’email’], ‘alphaNum’ => ’name’, ‘integer’ => ‘age’, ‘min’ => [[‘age’, 1]], ’email’ => ’email’ ];
We have four validation rules. The name and email are required. The name must be alphanumeric value, age must be integer and its minimal value is 1. Finally, the email must be a valid email address.
$ php multiple_rules.php Email is required Email is not a valid email address Name must contain only letters a-z and/or numbers 0-9
The example finished with three validation failures.
It is possible to add rules by chaining rule methods.
chaining.php
<?php
require(‘vendor/autoload.php’);
use Valitron\Validator;
$validator = new Validator([’name’ => ‘John Doe’, ’email’ => ‘johndoe#gmail.com’]); $validator->rule(‘required’, ’name’)->rule(’email’, ’email’);
if($validator->validate()) { echo “Validation passed”; } else {
$coll = collect($validator->errors());
$messages = $coll->flatten();
foreach ($messages as $message) {
echo $message . "\n";
}
}
The example chaines two rules.
$validator->rule(‘required’, ’name’)->rule(’email’, ’email’);
We add two validation rules by chaining rule methods.
There are four validation rules for dates: date, dateFormat, dateBefore and dateAfter.
date_before.php
<?php
require(‘vendor/autoload.php’);
use Valitron\Validator;
$validator = new Validator([‘created_at’ => ‘2019-03-01’]); $validator->rule(‘dateBefore’, ‘created_at’, ‘2018-10-13’);
if ($validator->validate()) { echo “Validation passed”; } else {
$coll = collect($validator->errors());
$messages = $coll->flatten();
foreach ($messages as $message) {
echo $message . "\n";
}
}
The example validates two dates using dateBefore rule.
$validator = new Validator([‘created_at’ => ‘2019-03-01’]); $validator->rule(‘dateBefore’, ‘created_at’, ‘2018-10-13’);
With the dateBefore rule, we validate that the given date is before some other date.
$ php date_before.php Created At must be date before ‘2018-10-13’
IP addresses are validated with the ip rule.
ipaddress.php
<?php
require ‘vendor/autoload.php’;
use Valitron\Validator;
$vals = [‘ip1’ => ‘127.0.0.1’, ‘ip2’ => ‘FE80:0000:0000:0000:0202:B3FF:FE1E:8329’, ‘ip3’ => ‘FE80::0202:B3FF:FE1E:8329’, ‘ip4’ => ‘0.0.1’];
$coll = collect($vals); $coll->each(function ($value, $key) {
$validator = new Validator([$key => $value]);
$validator->rule('ip', $key);
if ($validator->validate()) {
echo "Validation passed for $key with $value" . "\n";
} else {
$errs = collect($validator->errors());
$messages = $errs->flatten();
foreach ($messages as $message) {
echo $message . "\n";
}
}
});
The example validates IP v4 IP v6 addresses.
$ php ipaddress.php Validation passed for ip1 with 127.0.0.1 Validation passed for ip2 with FE80:0000:0000:0000:0202:B3FF:FE1E:8329 Validation passed for ip3 with FE80::0202:B3FF:FE1E:8329 Ip4 is not a valid IP address
We can provide custom validation messages. The messages are passed with
message.
custom_message.php
<?php
require(‘vendor/autoload.php’);
use Valitron\Validator;
$validator = new Validator([’name’ => ‘’]); $validator->rule(‘required’, ’name’)->message(’{field} is compulsory’)->label(“name”); $validator->rule(’lengthMin’, ’name’, 2)->message(’{field} must have at least 2 characters’) ->label(“name”);
if($validator->validate()) { echo “Validation passed”; } else {
$coll = collect($validator->errors());
$messages = $coll->flatten();
foreach ($messages as $message) {
echo $message . "\n";
}
}
The example adds custom messages.
$validator->rule(‘required’, ’name’)->message(’{field} is compulsory’)->label(“name”);
Using chained method calls, we add our custom validation message.
$ php custom_message.php name is compulsory name must have at least 2 characters
Subsets of values are validated with the subset rule.
subsets.php
<?php
require(‘vendor/autoload.php’);
use Valitron\Validator;
$vals = [‘colors’ => [‘green’, ‘blue’, ‘black’]];
$validator = new Validator($vals);
$validator->rule(‘subset’, ‘colors’, [‘red’, ‘green’, ‘blue’, ‘orange’, ‘yellow’]);
if ($validator->validate()) { echo “Validation passed”; } else {
$coll = collect($validator->errors());
$messages = $coll->flatten();
foreach ($messages as $message) {
echo $message . "\n";
}
}
The example checks if the $vals variable contains colours from the defined subset of colour values.
In the following example, we validate GET data.
get_data.php
<?php
require(‘vendor/autoload.php’);
use Valitron\Validator;
$validator = new Validator($_GET); $validator->rule(‘required’, [’name’, ’email’]); $validator->rule(’email’, ’email’);
if ($validator->validate()) { echo “Validation passed”; } else {
$coll = collect($validator->errors());
$messages = $coll->flatten();
foreach ($messages as $message) {
echo $message . "\n";
}
}
The example validates name and email parameters from a GET request.
$validator = new Validator($_GET);
The global $_GET variable is passed to the Validator.
$ php -S localhost:8000 PHP 7.2.11 Development Server started at Sat Feb 23 17:24:05 2019 Listening on http://localhost:8000 Document root is C:\Users\Jano\Documents\php-progs\valitron Press Ctrl-C to quit.
We start the built-in web server.
$ curl “localhost:8000/get_data.php?name=John%20Doe&email=john.doe#gmail.com” Email is not a valid email address
We create a GET request with two parameters with the curl tool.
In this article we have used Valitron to validate PHP values.
My name is Jan Bodnar, and I am a passionate programmer with extensive programming experience. I have been writing programming articles since 2007. To date, I have authored over 1,400 articles and 8 e-books. I possess more than ten years of experience in teaching programming.
List all PHP tutorials.