rougin/validia

A simple validation package in PHP.

dev-master 2025-08-24 09:48 UTC

This package is auto-updated.

Last update: 2025-08-24 09:48:14 UTC


README

A simple validation package based on Valitron.

Installation

Install the package using Composer:

$ composer require rougin/validia

Basic usage

The core of Validia is the Check class. To create a set of validation rules, create a PHP class that extends to Check:

use Rougin\Validia\Check;

class UserCheck extends Check
{
    /**
     * @var array<string, string>
     */
    protected $labels =
    [
        'age' => 'Age',
        'email' => 'Email',
        'name' => 'Name',
    ];

    /**
     * @var array<string, string>
     */
    protected $rules =
    [
        'age' => 'required|numeric',
        'email' => 'required|email',
        'name' => 'required',
    ];
}

The $labels property defines user-friendly names for the fields, which will be used in error messages:

use Rougin\Validia\Check;

class UserCheck extends Check
{
    /**
     * @var array<string, string>
     */
    protected $labels =
    [
        'age' => 'Age',
        'email' => 'Email',
        'name' => 'Name',
    ];

    // ...
}

While the $rules property specifies the validation rules for each field:

use Rougin\Validia\Check;

class UserCheck extends Check
{
    // ...

    /**
     * @var array<string, string>
     */
    protected $rules =
    [
        'age' => 'required|numeric',
        'email' => 'required|email',
        'name' => 'required',
    ];
}

Note

A list of available rules can be found in the Valitron documentation.

Once the Check class is created, it can be used to validate an array of data, such as data from a HTTP request:

$check = new UserCheck;

$data = /* e.g., data from a request */;

if (! $check->valid($data))
{
    // Get all available errors
    $errors = $check->errors();

    // Or get only the first error
    echo $check->firstError();

    return;
}

// Data has passed validation

Dynamic labels, rules

For more complex scenarios, the labels and rules methods can be overridden to define labels and rules dynamically:

use Rougin\Validia\Check;

class UserCheck extends Check
{
    /**
     * Returns the specified labels.
     *
     * @return array<string, string>
     */
    public function labels()
    {
        $this->labels['is_company'] = 'Is a Company?';

        return $this->labels;
    }

    /**
     * Returns the specified rules based on the data.
     *
     * @param array<string, mixed> $data
     *
     * @return array<string, string>
     */
    public function rules($data)
    {
        if (array_key_exists('is_company', $data))
        {
            $this->rules['company_name'] = 'required';
        }

        return $this->rules;
    }
}

Working with PSR-7 requests

If using ServerRequestInterface of PSR-7, the Request class provides a convenient way to validate request data:

use Rougin\Validia\Request;

class UserCheck extends Request
{
    /**
     * @var array<string, string>
     */
    protected $aliases =
    [
        'username' => 'name',
        'email_add' => 'email',
        'new_age' => 'age',
    ];

    // ...
}

The Request class provides two methods for validation: isParamsValid for validating query parameters and isParsedValid for validating the parsed body:

$check = new UserCheck;

// Should return the ServerRequestInterface ---
$request = Http::getServerRequest();
// --------------------------------------------

// Checks against data from "getQueryParams" ---
if ($check->isParamsValid($request))
{
    // Query parameters are valid
}
// ---------------------------------------------

// Checks against data from "getParsedBody" ---
if ($check->isParsedValid($request))
{
    // Parsed body is valid
}
// --------------------------------------------

When an alias is specified, it will be used to look for the field in the ServerRequestInterface data. For example, if the request data contains a username field, it will be validated against the rules defined for the name field.

Overriding the valid method

When extending the Request class and overriding the valid method, the setAlias method must be called to apply the defined aliases.

use Rougin\Validia\Request;

class UserCheck extends Request
{
    // ...

    public function valid($data)
    {
        // Always include this if aliases are defined ---
        $data = $this->setAlias($data);
        // ----------------------------------------------

        $valid = parent::valid($data);

        if (! $valid)
        {
            return count($this->errors) === 0;
        }

        // Add extra custom validation conditions here

        return count($this->errors) === 0;
    }
}

Change log

See CHANGELOG for more recent changes.

Development

Includes tools for code quality, coding style, and unit tests.

Code quality

Analyze code quality using phpstan:

$ phpstan

Coding style

Enforce coding style using php-cs-fixer:

$ php-cs-fixer fix --config=phpstyle.php

Unit tests

Execute unit tests using phpunit:

$ composer test