ZetCode

PHP Rakit Validation

last modified January 10, 2023

PHP Rakit Validation tutorial shows how to validate PHP values with Rakit Validation package.

$ php -v
php -v
PHP 8.1.2 (cli) (built: Aug  8 2022 07:28:23) (NTS)
...

We use PHP version 8.1.2.

Rakit Validation

Rakit Validation is a PHP Standalone Validation Library. It was inspired by Laravel's Illuminate\Validation.

Installation

$ composer require rakit/validation
$ composer require tightenco/collect

We install the Rakit Validation package and the Laravel's collection package.

Simple example

In the first example, we show how to do a very simple validation.

simple.php
<?php

require('vendor/autoload.php');

use Rakit\Validation\Validator;

$validator = new Validator;

$vals = ['name' => ''];
$rules = ['name' => 'required'];

$validation = $validator->make($vals, $rules);

$validation->validate();

if ($validation->fails()) {

    $coll = collect($validation->errors());

    $messages = $coll->flatten();

    foreach ($messages as $message) {
        echo $message . "\n";
    }
} else {

    echo "Validation passed";
}

The example validates one required value.

use Rakit\Validation\Validator;

We include the validator.

$validator = new Validator;

We create the instance of the Validator.

$vals = ['name' => ''];
$rules = ['name' => 'required'];

The $vals contains the values to be validated. The $rules contains the validation rules. In our case, the name value is required.

$validation = $validator->make($vals, $rules);

With the make method we prepare the validation; we pass the values and the rules.

$validation->validate();

We perform the validation with validate.

if ($validation->fails()) {

With fails we check if the validation failed.

$coll = collect($validation->errors());

$messages = $coll->flatten();

foreach ($messages as $message) {
    echo $message . "\n";
}

We use the Laravel's collections to parse the errors.

$ php simple.php
The Name is required

This is the output.

Validation rules

Rakit Validation contains a set of predefined rules, such as required, email, min, max, or url.

The rules can be combined with the | character.

rules.php
<?php

require 'vendor/autoload.php';

use Rakit\Validation\Validator;

$validator = new Validator;

$vals = ['name' => 'John Doe', 'email' => 'johndoe#gmail.com',
    'password' => '12345', 'confirm_password' => '23456'];

$rules = ['name' => 'required',
    'email' => 'required|email',
    'password' => 'required|min:6',
    'confirm_password' => 'required|same:password'];

$validation = $validator->make($vals, $rules);

$validation->validate();

if ($validation->fails()) {

    $coll = collect($validation->errors());

    $messages = $coll->flatten();

    foreach ($messages as $message) {
        echo $message . "\n";
    }
} else {

    echo "Validation passed";
}

The example uses several validation rules.

$rules = ['name' => 'required',
    'email' => 'required|email',
    'password' => 'required|min:6',
    'confirm_password' => 'required|same:password'];

We have four validation rules. The email is required must be a valid email address. The password is required and must have at least six characters. The confirm_password must be the same as the password.

$ php rules.php
The Email is not valid email
The Password minimum is 6
The Confirm password must be same with password

The example finished with three validation failures.

Validating dates

The next example shows how to validate dates.

dates.php
<?php

require('vendor/autoload.php');

use Rakit\Validation\Validator;

$validator = new Validator;

$vals = ['born' => '2000-03-30', 'meetingDate' => '2010-12-31'];
$rules = ['born' => 'before:2018-12-31', 'meetingDate' => 'after:2019-02-02'];

$validation = $validator->make($vals, $rules);

$validation->validate();

if ($validation->fails()) {

    $coll = collect($validation->errors());

    $messages = $coll->flatten();

    foreach ($messages as $message) {

        echo $message . "\n";
    }
} else {

    echo "Validation passed";
}

The example validates two dates.

$vals = ['born' => '2000-03-30', 'meetingDate' => '2010-12-31'];
$rules = ['born' => 'before:2018-12-31', 'meetingDate' => 'after:2019-02-02'];

With the before rule, we validate that the given date is before some date and with the after rule, we validate that the given date is after some date.

$ php dates.php
The MeetingDate must be a date after 2019-02-02.

This is the output.

Custom messages

We can provide custom validation messages. The messages are passed as the third parameter to the make method.

custom_message.php
<?php

require('vendor/autoload.php');

use Rakit\Validation\Validator;

$validator = new Validator;

$vals = ['name' => ''];
$rules = ['name' => 'required'];
$msgs = ['name' => 'The name is compulsory'];

$validation = $validator->make($vals, $rules, $msgs);

$validation->validate();

if ($validation->fails()) {

    $coll = collect($validation->errors());

    $messages = $coll->flatten();

    foreach ($messages as $message) {

        echo $message . "\n";
    }
} else {

    echo "Validation passed";
}

The example adds a custom message.

$msgs = ['name' => 'The name is compulsory'];

This is our custom message.

$validation = $validator->make($vals, $rules, $msgs);

The messages are passed to the make method.

$ php custom_message.php
The name is compulsory

This is the output.

Validating GET data

In the following example, we validate GET data.

get_data.php
<?php

require('vendor/autoload.php');

use Rakit\Validation\Validator;

$validator = new Validator;

$rules = ['name' => 'required', 'email' => 'required|email'];
$validation = $validator->make($_GET, $rules);

$validation->validate();

if ($validation->fails()) {

    $coll = collect($validation->errors());

    $messages = $coll->flatten();

    foreach ($messages as $message) {
        
        echo $message . "\n";
    }
} else {

    echo "Validation passed";
}

The example validates name and email parameters from a GET request.

$rules = ['name' => 'required', 'email' => 'required|email'];
$validation = $validator->make($_GET, $rules);

The make method takes the global $_GET variable as the first parameter.

$ php -S localhost:8000
[Wed Jul 13 15:08:56 2022] PHP 8.1.2 Development Server (http://localhost:8000) started

We start the built-in web server.

$ curl "localhost:8000/get_data.php?name=John%20Doe&email=john.doe#gmail.com"
The Email is not valid email

We create a GET request with two parameters with the curl tool.

In this article we have used Rakit Validation to validate PHP values.

Author

My name is Jan Bodnar and I am a passionate programmer with many years of programming experience. I have been writing programming articles since 2007. So far, I have written over 1400 articles and 8 e-books. I have over eight years of experience in teaching programming.

List all PHP tutorials.