2

I'm using Respect/Validation and I created the following rules to validate and associative array:

Validator::keySet(
        Validator::key( // mandatory, if included type: string, values: not null, not empty
            'name',
            Validator::stringType()->notEmpty()
        ),
        Validator::key( // mandatory, if included type: string, values: not null, not empty
            'company',
            Validator::stringType()->notEmpty()
        ),
        Validator::key( // mandatory, if included type: string, values: not null, not empty
            'type',
            Validator::stringType()->notEmpty()
        ),
        Validator::key( // mandatory, if included type: string, values: not null, not empty
            'country',
            Validator::stringType()->notEmpty()
        ),
        Validator::key( // optional, if included type: string, values: not null, not empty
            'comment',
            Validator::stringType()->notEmpty(),
            false
        )
    );

and when I validate an array, it works fine but if some of mandatory keys are missing (let's say "company" key) I always get an error message like:

- Must have keys { "name", "company", "type", "country", "comment" }

But I would like to customize the error messages and get something like:

"company" field is missing

I've tried:

$errors = $exception->findMessages([
...
'keyset' => '{{name}} field is missing',
....
]);

but {{name}} includes the whole array with key and values...

Is there any way to get that customize error message? should I include another {{placeholder}}?

Thanks in advance

Averias
  • 931
  • 1
  • 11
  • 20

1 Answers1

0

You can try some of the followings:

Validator::key( // mandatory, if included type: string, values: not null, not empty
        'company',
        Validator::stringType()->notEmpty()->setName('company')
    ),

Or

Validator::key( // mandatory, if included type: string, values: not null, not empty
        'company',
        Validator::stringType()->notEmpty()->setName('company')->setTemplate('"{{name}}" field is missing')
    ),

https://respect-validation.readthedocs.io/en/1.1/feature-guide/#validator-name

Validator name

On v::attribute() and v::key(), {{name}} is the attribute/key name. For others, is the same as the input. You can customize a validator name using:

v::date('Y-m-d')->between('1980-02-02', 'now')->setName('Member Since');

https://respect-validation.readthedocs.io/en/1.1/feature-guide/#exception-types

Exception types

Respect\Validation\Exceptions\ExceptionInterface:
    All exceptions implement this interface;
Respect\Validation\Exceptions\ValidationException:
    Implements the Respect\Validation\Exceptions\ExceptionInterface interface
    Thrown when the check() fails
    All validation exceptions extend this class
    Available methods:
        getMainMessage();
        setMode($mode);
        setName($name);
        setParam($name, $value);
        setTemplate($template);
        more...
Respect\Validation\Exceptions\NestedValidationException:
    Extends the Respect\Validation\Exceptions\ValidationException class
    Usually thrown when the assert() fails
    Available methods:
        findMessages();
        getFullMessage();
        getMessages();
        more...
Leandro Perini
  • 384
  • 5
  • 14