Skip to content

Error Wrapping

When validating nested payloads or structured request data, validation error keys must often match the exact dot-notation path of your input fields (e.g. data.name instead of name).

Pass a prefix string as the second argument to validate() to automatically wrap all thrown validation error keys with your desired prefix.

The validate method is available in all core classes:

Without wrapping

php
<?php

declare(strict_types=1);

use LaraPkgs\Validation\Support\Facades\Validatable;

$validatable = Validatable::collection(
    Validatable::item('name')->required(),
    Validatable::item('email')->required()
);

try {
    $validated = $validatable->validate([]);
} catch (ValidationException $e) {
    $errors = $e->errors();
    
    // [
    //     'name' => [
    //         'The name field is required.'
    //     ],
    //     'email' => [
    //         'The email field is required.'
    //     ]
    // ]
}

With wrapping

php
<?php

declare(strict_types=1);

use LaraPkgs\Validation\Support\Facades\Validatable;

$validatable = Validatable::collection(
    Validatable::item('name')->required(),
    Validatable::item('email')->required()
);

try {
    $validated = $validatable->validate([], 'data.');
} catch (ValidationException $e) {
    $errors = $e->errors();
    
    // [
    //     'data.name' => [
    //         'The name field is required.'
    //     ],
    //     'data.email' => [
    //         'The email field is required.'
    //     ]
    // ]
}

Trailing Dots

Trailing dots are automatically appended to the prefix, so passing data. or data produces the exact same result.