PHP Classes

How to Perform Validation of a Set of Values Using Laravel Validation Rules Without Using Laravel Laravel Like Form Validator: Validate an array of values using a set of rules

Recommend this page to a friend!
  Info   Documentation   View files Files   Install with Composer Install with Composer   Download Download   Reputation   Support forum   Blog    
Last Updated Ratings Unique User Downloads Download Rankings
2024-10-17 (Yesterday) RSS 2.0 feedNot enough user ratingsTotal: 1 This week: 1All time: 11,463 This week: 45Up
Version License PHP version Categories
nested-validator 1.0MIT/X Consortium ...8.2Validation, PHP 8
Description 

Author

This package can validate an array of values using a set of rules.

It takes as parameters an array of input values to validate and an array that defines the rules the input values must satisfy.

The package applies the validation rules and throws an exception when one of the rules is not satisfied.

Currently, it supports validation rules like:

- Non-empty strings

- Numeric values

- True boolean values

- Arrays of multiple values

- Values different from other values

- Custom validation using callback functions

- Etc...

Picture of András Zoltán-Gyárfás
Name: András Zoltán-Gyárfás <contact>
Classes: 8 packages by
Country: Romania Romania
Innovation award
Innovation award
Nominee: 4x

Winner: 1x

Documentation

The Laravel-like data validator

The Laravel-like Validator is a PHP validation library designed to help you validate data structures with ease. It supports various validation rules, custom rules, and nested data validation.

Installation

You can install the package via Composer:

composer require azolee/validator

Usage

For a complete list of available validation rules, please refer to the Validation Rules document.

Basic Usage

To use the validator, you need to define your validation rules and the data to be validated. Then, call the Validator::make method.

use Azolee\Validator\Validator;

$validationRules = [
    'user.name' => 'string',
    'user.age' => 'numeric',
    'user.is_active' => 'boolean',
    'address' => 'array',
    'address.city' => 'string',
    'address.street' => ['string', 'not_equals_field:address.city', 'not_equals_field:address.street2', 'not_equals_field:address.no'],
    'address.street2' => 'not_null',
    'address.no' => 'string',
    'images.*.url' => 'string',
    'images.*.role' => 'string',
];

$dataToValidate = [
    'user' => [
        'name' => 'John Doe',
        'age' => 30,
        'is_active' => true,
    ],
    'address' => [
        'city' => 'New York',
        'street' => 'First Avenue',
        'street2' => '',
        'no' => '52A',
    ],
    'images' => [
        [
            'url' => 'image1.jpg'
            'role' => 'profile_photo',
        ],
        [
            'url' => 'image2.jpg'
            'role' => 'album_photo',
            'description' => 'This is a photo of me.',
        ],
    ],
];

$result = Validator::make($validationRules, $dataToValidate);

if ($result->isFailed()) {
    echo "Validation failed!";
    print_r($result->getFailedRules());
} else {
    echo "Validation passed!";
}

Custom Rules

You can also define custom validation rules using closures.

$validationRules = [
    'user.name' => [
        function ($data) {
            return $data !== 'John Doe';
        },
        'string',
    ],
];

$dataToValidate = [
    'user' => [
        'name' => 'John Smith'
    ],
];

$result = Validator::make($validationRules, $dataToValidate);

if ($result->isFailed()) {
    echo "Validation failed!";
    print_r($result->getFailedRules());
} else {
    echo "Validation passed!";
}

Exception Handling

The validator can throw exceptions for invalid rules or data if the silent parameter is set to false.

use Azolee\Validator\Exceptions\InvalidValidationRule;
use Azolee\Validator\Exceptions\ValidationException;

try {
    $validationRules = [
        'name' => 123, // Invalid rule
    ];
    $dataToValidate = [
        'name' => 'John Doe',
    ];

    Validator::make($validationRules, $dataToValidate, false);
} catch (InvalidValidationRule $e) {
    echo "Caught an InvalidValidationRule exception: " . $e->getMessage();
} catch (ValidationException $e) {
    echo "Caught a ValidationException: " . $e->getMessage();
}

Testing

To run the tests, use PHPUnit:

vendor/bin/phpunit

License

This package is open-sourced software licensed under the MIT license.


  Files folder image Files (17)  
File Role Description
Files folder imagesrc (4 files, 2 directories)
Files folder imagetests (2 files)
Accessible without login Plain text file composer.json Data Auxiliary data
Accessible without login Plain text file composer.lock Data Auxiliary data
Accessible without login Plain text file LICENSE Lic. License
Accessible without login Plain text file phpunit.xml Data Auxiliary data
Accessible without login Plain text file Readme.md Doc. Documentation
Accessible without login Plain text file Rules.md Data Rules created

  Files folder image Files (17)  /  src  
File Role Description
Files folder imageExceptions (2 files)
Files folder imageHelpers (3 files)
  Plain text file ValidationError.php Class Class source
  Plain text file ValidationResult.php Class Class source
  Plain text file ValidationRules.php Class Class source
  Plain text file Validator.php Class Class source

  Files folder image Files (17)  /  src  /  Exceptions  
File Role Description
  Plain text file InvalidValidationRule.php Class Class source
  Plain text file ValidationException.php Class Class source

  Files folder image Files (17)  /  src  /  Helpers  
File Role Description
  Plain text file ArrayHelper.php Class Class source
  Plain text file ClassHelper.php Class Class source
  Plain text file CustomRules.php Class CustomRules created

  Files folder image Files (17)  /  tests  
File Role Description
  Plain text file ArrayHelperTest.php Class ArrayHelperTest created
  Plain text file ValidatorTest.php Class Class source

The PHP Classes site has supported package installation using the Composer tool since 2013, as you may verify by reading this instructions page.
Install with Composer Install with Composer
 Version Control Unique User Downloads Download Rankings  
 100%
Total:1
This week:1
All time:11,463
This week:45Up