rougin/validia

A simple validation package in PHP.

dev-master 2025-07-05 08:37 UTC

This package is auto-updated.

Last update: 2025-07-05 08:38:19 UTC


README

A simple validation package based on Valitron.

Installation

Install the package using Composer:

$ composer require rougin/validia

Basic usage

Create a class that extends to Check class:

use Rougin\Validia\Check;

class UserCheck extends Check
{
    protected $labels =
    [
        'age' => 'Age',
        'email' => 'Email',
        'name' => 'Name',
    ];

    protected $rules =
    [
        'age' => 'required|numeric',
        'email' => 'required|email',
        'name' => 'required',
    ];
}

Once created, submit the data to the said class for validation:

$check = new UserCheck;

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

if ($check->valid($data))
{
    // $data passed from validation
}
else
{
    // Get the available errors ---
    $errors = $check->errors();
    // ----------------------------

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

NOTE: Custom conditions for labels and rules is possible using the labels and rules methods:

use Rougin\Validia\Check;

class UserCheck extends Check
{
    public function labels()
    {
        // Add conditions to custom labels here ---
        // ----------------------------------------

        return $this->labels;
    }

    public function rules($data)
    {
        // Add conditions to custom rules here ---
        // ---------------------------------------

        return $this->rules;
    }
}

If using data from psr/http-message, kindly use the Request class instead and add aliases under alias if necessary:

use Rougin\Validia\Request;

class UserCheck extends Request
{
    protected $alias =
    [
        'name' => 'username',
        'email' => 'email_add',
        'age' => 'new_age',
    ];
}
$check = new UserCheck;

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

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

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

When extending from the Request class, kindly add the setAlias method when overriding the valid method to apply the aliases defined in the specified class:

use Rougin\Validia\Check;

class UserCheck extends Request
{
    // ...

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

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

        if (! $valid) return $valid;

        // Add extra conditions here ---
        // -----------------------------

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

NOTE: If an alias is specified, the aliases will be used in searching for the said fields from ServerRequestInterface.

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