Skip to content

Validatable Builder

The ValidatableBuilder defines the validation logic for a single item (e.g. attribute, field, property).

Instantiation

php
<?php

declare(strict_types=1);

use LaraPkgs\Validation\ValidatableBuilder;

$validatable = ValidatableBuilder::make('property');

The ValidatableBuilder can also be instantiated using the Facade or helper function. See the Facades & Helpers section here.

Add rules

Fluent Rules

Rules can be set using the fluent and chainable methods.

php
<?php

declare(strict_types=1);

use LaraPkgs\Validation\ValidatableBuilder;

$validatable = ValidatableBuilder::make('property')->required();

A complete list of available rules can be found here.

Rule Objects

php
<?php

declare(strict_types=1);

use Illuminate\Validation\Rule;
use LaraPkgs\Validation\ValidatableBuilder;

$validatable = ValidatableBuilder::make('property')->applyRule(Rule::numeric());

Rule Precedence

Rules are sorted by priority (modifier -> circuit -> presence -> type -> constraint) before evaluation, preventing unpredictable behavior with rules like bail.

This only works with Fluent Rules and not with Rule Objects.

More detailed information about rule precedence can be found here

Add custom messages

php
<?php

declare(strict_types=1);

use LaraPkgs\Validation\ValidatableBuilder;

$validatable = ValidatableBuilder::make('property')->required()
    ->addMessages(['required' => 'Custom required message.']);

Set custom attribute

php
<?php

declare(strict_types=1);

use LaraPkgs\Validation\ValidatableBuilder;

$validatable = ValidatableBuilder::make('property')->required()
    ->setCustomAttribute('custom');

Immutability

The ValidatableBuilder is 100% immutable so every mutation returns a new object. Rule objects will also be cloned.

php
<?php

declare(strict_types=1);

use LaraPkgs\Validation\ValidatableBuilder;

$validatable = ValidatableBuilder::make('property');
$mutated = $validatable->required();
$isSame = $validatable === $mutated; // false

$mutated = $validatable->addMessages(['required' => 'Custom required message.']);
$isSame = $validatable === $mutated; // false

$mutated = $validatable->setCustomAttribute('custom');
$isSame = $validatable === $mutated; // false

Validation methods

validate(array $data)

Tries to validate the given data and throws an Illuminate\Validation\ValidationException when it fails and an array of validated data if it passes.

php
<?php

declare(strict_types=1);

use LaraPkgs\Validation\ValidatableBuilder;

$validatable = ValidatableBuilder::make('property')->required()->numeric()->min(10);

$data = [];
$validated = $validatable->validate($data); // Throws Illuminate\Validation\ValidationException;

$data = ['property' => 100];
$validated = $validatable->validate($data); // ['property' => 100];

fails(array $data)

Indicates if the given data will fail validation.

php
<?php

declare(strict_types=1);

use LaraPkgs\Validation\ValidatableBuilder;

$validatable = ValidatableBuilder::make('property')->required()->numeric()->min(10);

$data = [];
$fails = $validatable->fails($data); // true

$data = ['property' => 100];
$fails = $validatable->fails($data); // false

passes(array $data)

Indicates if the given data will pass validation.

php
<?php

declare(strict_types=1);

use LaraPkgs\Validation\ValidatableBuilder;

$validatable = ValidatableBuilder::make('property')->required()->numeric()->min(10);

$data = [];
$passes = $validatable->passes($data); // false

$data = ['property' => 100];
$passes = $validatable->passes($data); // true

toValidatorArguments()

Returns an array of arguments (rules, messages and attributes) that can be used to instantiate a Laravel Validator.

php
<?php

declare(strict_types=1);

use LaraPkgs\Validation\ValidatableBuilder;

$validatable = ValidatableBuilder::make('property')->required()
    ->addMessages(['required' => 'Custom required message.'])
    ->setCustomAttribute('custom');

$arguments = $validatable->toValidatorArguments();

// [
//     'rules' => [
//         'property' => ['required'],
//     ],
//     'messages' => [
//         'property.required' => 'Custom required message.',
//     ],
//     'attributes' => [
//         'property' => 'custom',
//     ]
// ]

makeValidator(array $data)

Instantiates a Laravel Validator for the given data.

php
<?php

declare(strict_types=1);

use LaraPkgs\Validation\ValidatableBuilder;

$validatable = ValidatableBuilder::make('property')->required();

$data = ['property' => 'value'];
$validator = $validatable->makeValidator($data); // Illuminate\Contracts\Validation\Validator;