2
votes

I would like to ask for some advices about Laravel Validation...

Let's say I've got an input named invoiceAddress[name] and in a controller I've got a rule

$rule = ['invoiceAddress.name' => 'required',];

or just a

$validator = \Validator::make($request->all(), [
    'invoiceAddress.name' => 'required',
]);

now, inside custom validation language file validation.php, am I able to nest attributes somehow? like:

'required' => ':attribute is mandatory',

'attributes' => [
    'invoiceAddress' => [
        'name' => 'blahblah'
    ],
],

If I try to nest the attribute the way above, i get

ErrorException
    mb_strtoupper() expects parameter 1 to be string, array given

because I am using a field (as above)

['name' => 'blahblah']

I am trying to get custom messages using the file and the :attribute directive (as mentioned in the code above).

I am basically trying to do this How to set custom attribute labels for nested inputs in Laravel but i get the mentioned error...

Thank you in advance...

1
what is validation.php what are you trying to achieve are you having problem in message of validation? - Sagar Rabadiya
validation.php is my custom validation language file and I am trying to get a message using defined attributes and :attribute directive ... instead of defining hundreds of custom messages - Jan Vávra
This issue still exists, have you found any valid solution? - Desh901

1 Answers

1
votes

A Note On Nested Attributes

If your HTTP request contains "nested" parameters, you may specify them in your validation rules using "dot" syntax:

$this->validate($request, [
    'title' => 'required|unique:posts|max:255',
    'author.name' => 'required',
    'author.description' => 'required',
]);

Reference: https://laravel.com/docs/5.3/validation#quick-writing-the-validation-logic (A Note On Nested Attributes Section)