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-24 (4 hours ago) RSS 2.0 feedNot enough user ratingsTotal: 14 This week: 14All time: 11,382 This week: 2Up
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 for PHP

Latest Version

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.email' => ['email', 'not_null'],
    'user.website' => ['url'],
    'user.password' => ['password:ulds', 'min:8'],
    'user.password_confirmation' => ['same:user.password'],
    'user.is_active' => ['boolean', 'not_null'],
    'address' => 'array',
    'address.city' => 'string',
    'address.street' => ['string', 'different:address.city', 'different:address.street2', 'different:address.no'],
    'address.street2' => 'not_null',
    'address.no' => 'string',
    'images.*.url' => 'string',
    'images.*.role' => ['string', 'in:profile_photo,album_photo'],
];

$dataToValidate = [
    'user' => [
        'name' => 'John Doe',
        'email' => 'user@email.com',
        'password' => 'SecretPasswd#1',
        'password_confirmation' => 'secret',
        'website' => 'https://github.com',
        '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::config(['silent' => false])->make($validationRules, $dataToValidate);
} catch (InvalidValidationRule $e) {
    echo "Caught an InvalidValidationRule exception: " . $e->getMessage();
} catch (ValidationException $e) {
    echo "Caught a ValidationException: " . $e->getMessage();
}

Additional Examples

For more detailed examples, please refer to the following documents: - Simple Examples - Complex Examples

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 (27)  
File Role Description
Files folder imagedocs (3 files)
Files folder imagesrc (6 files, 3 directories)
Files folder imagetests (5 files, 1 directory)
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

  Files folder image Files (27)  /  docs  
File Role Description
  Accessible without login Plain text file ComplexExamples.md Data Auxiliary data
  Accessible without login Plain text file Rules.md Data Auxiliary data
  Accessible without login Plain text file SimpleExamples.md Data Auxiliary data

  Files folder image Files (27)  /  src  
File Role Description
Files folder imageContracts (1 file)
Files folder imageExceptions (2 files)
Files folder imageHelpers (4 files)
  Plain text file ValidationErrorBag.php Class Class source
  Plain text file ValidationErrorManager.php Class Class source
  Plain text file ValidationResult.php Class Class source
  Plain text file ValidationRuleEvaluator.php Class Class source
  Plain text file ValidationRules.php Class Class source
  Plain text file Validator.php Class Class source

  Files folder image Files (27)  /  src  /  Contracts  
File Role Description
  Plain text file ValidationErrorBagInterface.php Class Class source

  Files folder image Files (27)  /  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 (27)  /  src  /  Helpers  
File Role Description
  Plain text file ArrayHelper.php Class Class source
  Plain text file ClassHelper.php Class Class source
  Plain text file ErrorBags.php Class Class source
  Plain text file ErrorsHelper.php Class Class source

  Files folder image Files (27)  /  tests  
File Role Description
Files folder imageHelpers (1 file)
  Plain text file ArrayHelperTest.php Class ArrayHelperTest created
  Plain text file CustomValidationErrorBagTest.php Class Class source
  Plain text file ValidatorExamplesTest.php Class Class source
  Plain text file ValidatorTest.php Class Class source
  Plain text file ValidatorWithCustomErrorBagTest.php Class Class source

  Files folder image Files (27)  /  tests  /  Helpers  
File Role Description
  Plain text file CustomRules.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:14
This week:14
All time:11,382
This week:2Up