how to translate Nested Attributes on laravel validation?

629 Views Asked by At

I have a problem translating nested attributes on laravel validation, the code im using is like:

$this->validate($request,[
'title.*' => 'required'
]);

and * can be: en, es, fa ... and other languages too

the reason doing that is because we dont know the exact languages used in website, it can be changed from adminpanel, and we need to add for example "title" of product for each language. like:

'title.en' => 'required'
'title.fa' => 'required'
'title.es' => 'required'

which code below almost do the same:

$this->validate($request, [
    'title.*' => 'required',
]);

and everything works just fine about this code, but the problem is about translating those fields.
the message of validation is like:

"title.en is required."

however i added en, fa .. and every single lang on validation.php like:

'attributes' => [
    'title' => 'TITLE', ###"TITLE" simply is for translation of title for other languages
    'en' => 'ENGLISH',
    'fa' => 'FARSI',
]

and its not showing message like this:

"TITLE ENGLISH is required."

and its not possible to write like this:

'attributes' => [
    'title.en' => 'ENGLISH TITLE',
    'title.fa' => 'FARSI TITLE',
]

i tried using arrays in attributes like:

'attributes' => [
    'title' => 'TITLE',
    'title' => [
        'en' => 'English Title',
        'fa' => 'Farsi Title',
    ]
]

but that causes error because title defined 2 times as an array and string !!

I would be glad if someone can help me.
thanks, and sorry for english :))

1

There are 1 best solutions below

0
On

You can change attribute names for validation messages by adding attributes array in your validation.php like this

[
  //validation messages

  'attributes' => [
     'title.en' => 'Title in English'
  ],
]

And instead of title.en is required. the message should be Title in English is required.