Download the PHP package selective/validation without Composer
On this page you can find all versions of the php package selective/validation. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download selective/validation
More information about selective/validation
Files in selective/validation
Package validation
Short Description Validation error collector and transformer
License MIT
Informations about the package validation
Validation
A validation library for PHP that uses the notification pattern.
Table of contents
- Requirements
- Installation
- Usage
- Validating form data
- Validating JSON
- Regex
- Validators
- CakePHP Validator
- Transformer
- License
Requirements
- PHP 8.1+
Installation
Usage
A notification is a collection of errors
In order to use a notification, you have to create the ValidationResult
object. A ValidationResult
can be really
simple:
You can now test the ValidationResult
and throw an exception if it contains errors.
Validating form data
Login example:
Validating JSON
Validating a JSON request works like validating form data, because in PHP it's just an array from the request object.
In vanilla PHP you can fetch the JSON request as follows:
Regex
The Selective\Validation\Regex\ValidationRegex
class allows you to validate if
a given string conforms a defined regular expression.
Example usage:
Middleware
The ValidationExceptionMiddleware
catches the ValidationException
and converts it into a nice JSON response.
Slim 4 integration
Insert a container definition for ValidationExceptionMiddleware::class
:
Add the ValidationExceptionMiddleware
into your middleware stack:
Usage in Slim
Validators
You can combine this library with a validator that is doing the actual validation of your input data.
The converter pattern makes it easy to map instances of one class into instances of another class.
CakePHP Validator
The cakephp/validation library provides features to build validators that can validate arbitrary arrays of data with ease.
Installation
Usage
The Cake\Validation\Validator::validate()
method returns a non-empty array when
there are validation failures. The list of errors then can be converted into
a ValidationResult
using the Selective\Validation\Factory\CakeValidationFactory
or Selective\Validation\Converter\CakeValidationConverter
.
For example, if you want to validate a login form you could do the following:
Please note: The CakeValidationFactory
should be injected via constructor.
Read more: https://odan.github.io/2020/10/18/slim4-cakephp-validation.html
Transformer
If you want to implement a custom response data structure,
you can implement a custom transformer against the
\Selective\Validation\Transformer\ResultTransformerInterface
interface.
Example
License
The MIT License (MIT). Please see License File for more information.
All versions of validation with dependencies
ext-json Version *
psr/http-factory Version ^1.0.1
psr/http-server-middleware Version ^1.0.1