PHP Classes

PHP Array Validator: Validate an array of values with a set of rules

Recommend this page to a friend!
  Info   View files Documentation   View files View files (22)   DownloadInstall with Composer Download .zip   Reputation   Support forum   Blog    
Last Updated Ratings Unique User Downloads Download Rankings
2022-09-01 (3 days ago) RSS 2.0 feedNot yet rated by the usersTotal: 49 This week: 14All time: 10,336 This week: 13Up
Version License PHP version Categories
array-validator 1.0GNU General Publi...7.4Validation, PHP 7
Description Author

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

It provides a base class to validate a set of values passed to the class constructor as an array of values.

Applications can extend this class and pass a specific set of validation rules by passing a given array of validation rules.

A separate validation class implements each validation rule.

The package provides a base type validation class, as well several kinds of validation classes for validating values of types:

- Date and time

- Enumerated values

- Integers

- Strings

- Maximum and minimum limit values

- Regular expressions

Picture of Krisztián Dudás
  Performance   Level  
Name: Krisztián Dudás <contact>
Classes: 2 packages by
Country: Hungary Hungary
Innovation award
Innovation award
Nominee: 1x

Winner: 1x

Details

array-validator

Validates an array of values with a set of validators

Example validator:

class DemoValidator extends ValidatorChain
{

    public function __construct()
    {
        $validators = [
            'phone' => [
                'required' => true,
                'validators' => [
                    [
                        'type' => Regexp::class,
                        //The message attribute changes the default message of the validator
                        'message' => 'Invalid phone number provided',
                        'options' => [
                            'pattern' => '/\+36-\d{2}[-]\d{3}[-]\d{4}\b/'
                        ]
                    ],
                    [
                        'type' => IsString::class
                    ]
                ]
            ],
            'name' => [
                'required' => true,
                //The Required message attribute changes the default message omitted when a field is required, but is empty or not present
                'requiredMessage' => 'This field is required',
                'validators' => [
                    [
                        'type' => IsString::class
                    ]
                ]
            ],
            'status' => [
                'required' => true,
                'validators' => [
                    [
                        'type' => IsString::class
                    ],
                    [
                        'type' => Enum::class,
                        'options' => [
                            'allowedElements' => ['active', 'inactive', 'deleted']
                        ]
                    ]
                ]
            ]
        ];

        parent::__construct($validators);
    }

}

You can also create a new instance of the KDudas\ArrayValidator\ValidatorChain with the same parameters in its __construct as above and call its isValid method. If you want to add new validation logic, simply implement the KDudas\ArrayValidator\ValidatorInterface in a class and it is ready to use. To perform the validation, simply call the isValid method on the validator instance. To get validation messages, call the getMessages method on the ValidatorChain instance.

  Files folder image Files  
File Role Description
Files folder imagesrc (11 files)
Files folder imagetests (8 files)
Accessible without login Plain text file composer.json Data Auxiliary data
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  /  src  
File Role Description
  Plain text file DateTimeValidator.php Class Class source
  Plain text file Enum.php Class Class source
  Plain text file IsFloat.php Class Class source
  Plain text file IsInteger.php Class Class source
  Plain text file IsNumeric.php Class Class source
  Plain text file IsString.php Class Class source
  Plain text file MinMax.php Class Class source
  Plain text file Regexp.php Class Class source
  Plain text file ValidatorBase.php Class Class source
  Plain text file ValidatorChain.php Class Class source
  Plain text file ValidatorInterface.php Class Class source

  Files folder image Files  /  tests  
File Role Description
  Plain text file DateTimeTest.php Class Class source
  Plain text file EnumTest.php Class Class source
  Plain text file IsFloatTest.php Class Class source
  Plain text file IsIntegerTest.php Class Class source
  Plain text file IsStringTest.php Class Class source
  Plain text file MinMaxTest.php Class Class source
  Plain text file RegexpTest.php Class Class source
  Plain text file ValidatorChainTest.php Class Class source

 Version Control Unique User Downloads Download Rankings  
 100%
Total:49
This week:14
All time:10,336
This week:13Up
For more information send a message to info at phpclasses dot org.