https://github.com/rougin/valla
A simple validation package in PHP.
https://github.com/rougin/valla
php-validation php-validator
Last synced: 16 days ago
JSON representation
A simple validation package in PHP.
- Host: GitHub
- URL: https://github.com/rougin/valla
- Owner: rougin
- License: mit
- Created: 2025-07-05T08:24:23.000Z (7 months ago)
- Default Branch: master
- Last Pushed: 2025-10-26T02:35:23.000Z (3 months ago)
- Last Synced: 2025-10-26T04:19:42.353Z (3 months ago)
- Topics: php-validation, php-validator
- Language: PHP
- Homepage:
- Size: 16.6 KB
- Stars: 0
- Watchers: 0
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE.md
Awesome Lists containing this project
README
# Valla
[![Latest Version on Packagist][ico-version]][link-packagist]
[![Software License][ico-license]][link-license]
[![Build Status][ico-build]][link-build]
[![Coverage Status][ico-coverage]][link-coverage]
[![Total Downloads][ico-downloads]][link-downloads]
A simple validation package based on [Valitron](https://github.com/vlucas/valitron).
``` php
use Rougin\Valla\Check;
class UserCheck extends Check
{
protected $labels = array(
'name' => 'Name',
'email' => 'Email',
);
protected $rules = array(
'name' => 'required',
'email' => 'required|email',
);
}
```
## Installation
Install the package using [Composer](https://getcomposer.org/):
``` bash
$ composer require rougin/valla
```
## Basic usage
The core of `Valla` is the `Check` class which is used to create a set of validation rules:
``` php
use Rougin\Valla\Check;
class UserCheck extends Check
{
/**
* @var array
*/
protected $labels =
[
'age' => 'Age',
'email' => 'Email',
'name' => 'Name',
];
/**
* @var array
*/
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:
``` php
use Rougin\Valla\Check;
class UserCheck extends Check
{
/**
* @var array
*/
protected $labels =
[
'age' => 'Age',
'email' => 'Email',
'name' => 'Name',
];
// ...
}
```
While the `$rules` property specifies the validation rules for each field:
``` php
use Rougin\Valla\Check;
class UserCheck extends Check
{
// ...
/**
* @var array
*/
protected $rules =
[
'age' => 'required|numeric',
'email' => 'required|email',
'name' => 'required',
];
}
```
> [!NOTE]
> A list of available rules can be found in the [Valitron documentation](https://github.com/vlucas/valitron#validation-rules).
Once the `Check` class is created, it can be used to validate an array of data, such as data from a HTTP request:
``` php
$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
```
## Labels, rules
For more complex scenarios, the `labels` and `rules` methods can be overridden to define them dynamically:
``` php
use Rougin\Valla\Check;
class UserCheck extends Check
{
/**
* Returns the specified labels.
*
* @return array
*/
public function labels()
{
$this->labels['is_company'] = 'Is a Company?';
return $this->labels;
}
/**
* Returns the specified rules based on the data.
*
* @param array $data
*
* @return array
*/
public function rules($data)
{
if (array_key_exists('is_company', $data))
{
$this->rules['company_name'] = 'required';
}
return $this->rules;
}
}
```
## Using PSR-7 requests
If using `ServerRequestInterface` from [PSR-7](https://www.php-fig.org/psr/psr-7/), the `Request` class provides a convenient way to validate request data:
``` php
use Rougin\Valla\Request;
class UserCheck extends Request
{
/**
* @var array
*/
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:
``` php
$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:
``` php
use Rougin\Valla\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;
}
}
```
## Changelog
Please see [CHANGELOG][link-changelog] for more recent changes.
## Contributing
See [CONTRIBUTING][link-contributing] on how to contribute to the project.
## License
The MIT License (MIT). Please see [LICENSE][link-license] for more information.
[ico-build]: https://img.shields.io/github/actions/workflow/status/rougin/valla/build.yml?style=flat-square
[ico-coverage]: https://img.shields.io/codecov/c/github/rougin/valla?style=flat-square
[ico-downloads]: https://img.shields.io/packagist/dt/rougin/valla.svg?style=flat-square
[ico-license]: https://img.shields.io/badge/license-MIT-brightgreen.svg?style=flat-square
[ico-version]: https://img.shields.io/packagist/v/rougin/valla.svg?style=flat-square
[link-build]: https://github.com/rougin/valla/actions
[link-changelog]: https://github.com/rougin/valla/blob/master/CHANGELOG.md
[link-contributing]: https://github.com/rougin/valla/blob/master/CONTRIBUTING.md
[link-coverage]: https://app.codecov.io/gh/rougin/valla
[link-downloads]: https://packagist.org/packages/rougin/valla
[link-license]: https://github.com/rougin/valla/blob/master/LICENSE.md
[link-packagist]: https://packagist.org/packages/rougin/valla