PHP Classes

PHP Array Validator: Validate an array of values with 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    
Ratings Unique User Downloads Download Rankings
Not yet rated by the usersTotal: 63 All time: 10,443 This week: 660Up
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  
Innovation award
Innovation award
Nominee: 1x

Winner: 1x

 

Documentation

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 (22)  
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

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:63
This week:0
All time:10,443
This week:660Up