Laravel Validator Object-Oriented Wrapper

Laravel Validator Object-Oriented Wrapper

The Laravel Validator object-oriented wrapper package by Krzysztof Rewak provides a map of all string rules into chained methods. The builder helps you write object-oriented style rules that translate back into their string counterparts:

$validator = new ValidationBuilder();
$validator->validate("email", function (Field $field): void {
    $field->string()->required()->email(["rfc"])
});

$validator->getRules();
// ["email" => "string|required|email:rfc]

Here’s an example of nested validation rules:

$validator = new ValidationBuilder();
$validator->validateEach("tags", function (Field $field): void {
    $field->array();
});
$validator->validateInEach("id", "tags", function (Field $field): void {
    $field->required()->exists("tags", "id");
});

$validator->getRules();
// ["tags.*:" => "array", "tags.*.id:" => "required|exists:tags,id"]

And finally, you can also use the builder in form requests:

/**
 * @return array
 */
public function rules(): array
{
    $this->builder->validate("email", function (Field $field): void {
        $field->required()->string()->email()->unique("users", "email");
    });
    $this->builder->validate("password", function (Field $field): void {
        $field->required()->string()->min(6)->confirmed();
    });

    return $this->builder->getRules();
}

You can learn more about this package, get full installation instructions, and view the source code on GitHub at krzysztofrewak/laravel-oop-validator.

This package was submitted to our Laravel News Links section. Links is a place the community can post packages and tutorials around the Laravel ecosystem. Follow along on Twitter @LaravelLinks


Filed in: News / packages


Newsletter

Join the weekly newsletter and never miss out on new tips, tutorials, and more.

Laravel News Partners

Laravel Jobs

Laravel Developer
San Jose, CA - Must Be Local
X3 Builders
Experienced PHP Developer
Wilmington, NC
Queensboro Shirt Company
Senior Laravel developer
Netherlands, Rotterdam
Pionect
Senior Laravel Engineer
Remote okay (must already live in USA)
Hawthorne Effect
Senior Software Engineer (Remote - Contract)
Remote
Koodi Systems
PHP Developer
Pittsburgh / Remote
Sequoia Waste Solutions
Software Developer
Eindhoven
Simac IDS