Reputation: 21
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 :))
Upvotes: 2
Views: 939
Reputation: 2604
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
.
Upvotes: 0