raoul/php-validator

A simple validator for PHP

V1.1.0 2023-03-06 20:00 UTC

This package is auto-updated.

Last update: 2024-05-06 22:29:25 UTC


README

How to install

composer require raoul/php-validator

Or

git clone https://github.com/RaoulvanWijk/Validator.git

How to use it?

To use my validator you can either instantiate the validator class

require_once __DIR__ .'/vendor/autoload.php';

use Raoul\Validator\Validator;

$validator = new Validator();

or create a new class that extends the base validator class

require_once __DIR__ .'/vendor/autoload.php';

use Raoul\Validator\Validator;

Class CustomValidator extends Validator
{
    public function rules()
    {
        return [
            // Your rules go here
        ];
    }

    public function messages()
    {
        return [
            // Your custom messages go here
        ];
    }
}

$validator = new CustomValidator();

And then call the validate method with the needed data

$validator->validate($data);

Specifying the rules

You have 2 options when specifying the rules when calling the validate method

$validator->validate($data, [
  'name' => ['required', 'min:4'],
  'email' => 'required|email'
  ]);

or in the rules() method

public function rules()
    {
        return [
            'name' => ['required', 'min:4'],
            'email' => 'required|email'
        ];
    }

Specifying custom validation messages

available validation rules