Skip to content
/ valid8 Public

A simple validator for your PHP project without dependencies.

Notifications You must be signed in to change notification settings

xoesae/valid8

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

15 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Valid8: a PHP package for validation

Validations in a simple and fast way. 🐘

Installation

In progress 🚧 👷

Usage

Using Valid8 is easy, it consists of instantiating the class passing its data array and an array of rules

$validator = new \Valid8\Validator(
    ['name' => 'xoesae'], // data
    ['name' => ['required']], // rules
);

$isValid = $validator->validate();

if ($isValid) {
    // ...
} else {
    // Some error happened
    $errors = $validator->errors();
    var_dump($errors);
}

Another valid8 approach is to extend the validator class to create a custom validator:

class UserValidator extends \Valid8\Validator
{
    protected static function rules(): array
    {
        return [
            'name' => ['required'],
        ];
    }
    
    protected static function messages(): array
    {
        return [
            'required' => '{field} is required.'
        ];
    }

    protected static function fields(): array
    {
        return [
            'name' => 'name',
        ];
    }
}

$validator = new UserValidator($data);
$validator->validate()
$validator->errors();

It's also easy to add new customizable rules, so need implements the \Valid8\Rules\Rule interface:

class CustomRule implements \Valid8\Rules\Rule
{
    protected string $field;

    public function validate(string $field, mixed $data): bool
    {
        $this->field = $field;

        return is_int($data);
    }

    public function error(): string
    {
        return "{$this->field} must be equal to an integer.";
    }
}

$validator = new \Valid8\Validator(
    ['name' => 'xoesae'], // data
    ['name' => [new CustomRule()]], // rules
);
$validator->validate()
$validator->errors();

working with customizable error messages:

  • Use '{field}' to replace the name of field
  • If is a custom rule, you can customize the message template however you want
$validator = new \Valid8\Validator(
    ['name' => 'xoesae'], // data
    ['name' => ['required', new CustomRule()]], // rules
    [
        'required' => '{field} is missing.',
        CustomRule::class => '{field} not is a int'
    ] // Custom error messages
);
$validator->validate()
$validator->errors();

Default rules

  • Required: verify if field is present and not empty.
  • Email: verify if field is a valid email address.

Contributing

Make a Pull Request for the repository, adding its functionality along with the tests. Make sure your change doesn't break previous versions. 😄

About

A simple validator for your PHP project without dependencies.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages