marcino0o / php-validator
Advanced validation package
v1.2.8
2023-09-30 09:20 UTC
Requires
- php: ^8.1
- ext-mbstring: *
Requires (Dev)
- phpstan/phpstan: ^1.10
- phpunit/phpunit: ^9
- squizlabs/php_codesniffer: ^3.7
This package is auto-updated.
Last update: 2025-03-29 01:06:23 UTC
README
PHP package for easy validation
Features
- field sets validation
- custom error messages
Requirements
- PHP >= 8.1
- composer
Installation
Using composer:
composer require "marcino0o/php-validator"
Usage
Quick example
<?php require('vendor/autoload.php'); use Validator\FieldSetValidator; use Validator\Field; use Validator\Rule\Email; use Validator\Rule\TypeString; $dataToValidate = [ 'email' => 'joe.doe@example.com', // will pass 'name' => null, // will pass ]; $validator = new FieldSetValidator( Field::required('email', new Email()), Field::optional('name', new TypeString())->nullable(), ); if ($validator->validate($dataToValidate)->hasErrors()) { var_dump($validator->getErrors()); exit; } // all good
Specifying allowed fields in set
<?php require('vendor/autoload.php'); use Validator\FieldSetValidator; use Validator\Field; use Validator\Rule\Email; $dataToValidate = [ 'email' => 'joe.doe@example.com', // will pass 'name' => 'Joe Doe', // will not pass ]; $validator = new FieldSetValidator( Field::required('email', new Email()), ); $validator->withAllowedFields('email');
Fields in sets with multidimensional arrays
<?php require('vendor/autoload.php'); use Validator\FieldSetValidator; use Validator\Field; use Validator\Rule\TypeArray use Validator\Rule\TypeString; use Validator\Rule\Uuid; $dataToValidate = [ 'author' => [ 'uuid' => 'c07f9405-8618-49a7-980a-e4982e307274', 'name' => 'Joe Doe', ], ]; $validator = new FieldSetValidator( Field::required('author', (new TypeArray())->withRequiredKeys('uuid', 'name')), Field::requiredWith('author.uuid', 'author' new Uuid()), Field::requiredWith('author.name', 'author' new TypeString()), );
Field requirement options
Always required
<?php require('vendor/autoload.php'); use Validator\Field; use Validator\Rule\TypeString; // example 1 Field::required('text', new TypeString()) ->validate(['text' => 'Hello world']) ->hasErrors(); // will return false // example 2 Field::required('text', new TypeString()) ->validate([]) ->hasErrors(); // will return true
Optional
<?php require('vendor/autoload.php'); use Validator\Field; use Validator\Rule\TypeString; // example 1 Field::optional('text', new TypeString()) ->validate(['text' => 'Hello world']) ->hasErrors(); // will return false // example 2 Field::optional('text', new TypeString()) ->validate([]) ->hasErrors(); // will return false
Required with other field
<?php require('vendor/autoload.php'); use Validator\Field; use Validator\Rule\TypeString; // example 1 Field::requiredWith('i_will_be_required', 'when_i_exists', new TypeString()) ->validate(['when_i_exists' => 'Hello world']) ->hasErrors(); // will return true // example 2 Field::requiredWith('i_will_be_required', 'when_i_exists', new TypeString()) ->validate([ 'i_will_be_required' => 'Hello universe', 'when_i_exists' => 'Hello world', ]) ->hasErrors(); // will return false // example 3 Field::requiredWith('i_will_be_required', 'when_i_exists', new TypeString()) ->validate(['not_related_param' => 'Hello world']) ->hasErrors(); // will return false
Custom messages
<?php require('vendor/autoload.php'); use Validator\Dictionary\TypeStringDictionary as Dictionary; use Validator\Rule\TypeString; $example1 = new TypeString; $example1->withMessages([ Dictionary::LENGTH_TOO_SHORT => 'C\'mon, {{ minLength }} characters it\'s not much! You can write more than "{{ value }}"', Dictionary::LENGTH_TOO_LONG => 'Take it easy! There is a space for only {{ maxLength }} characters!', ]); $example1->lengthBetween(5, 1000)->isSatisfiedBy('abc'); // false $example1->getErrors()->first()->getMessage(); // C'mon, 5 characters it's not much! You can write more than "abc" $example2 = new TypeString; $example1->withMessage(Dictionary::LENGTH_TOO_SHORT, 'Text is too short.')