I have two inputs in a blade view from an array (each of them for diferent languages) and I build like this:
@foreach ($langs as $lang)
<input
type="text"
name="title[{{$lang->isocode}}]"
id="title[{{$lang->idioma->codi}}]"
value="{{ $lang->title }}"
>
@endforeach
My custom Form Request Validation is this:
public function rules()
{
return [
'title.*' => ['required', 'string', 'max:100'],
];
}
public function messages()
{
return [
'title.*.required' => 'Title is required',
'title.*.string' => 'Title must be a string',
'title.*.max' => 'Title is too long',
];
}
and it works fine. In fact, if I @dump($errors)
, it shows:
Illuminate\Support\ViewErrorBag {#353 ▼
#bags: array:1 [▼
"default" => Illuminate\Support\MessageBag {#354 ▼
#messages: array:1 [▼
"title.ca" => array:1 [▼
0 => "Title is too long"
]
]
#format: ":message"
}
]
}
But the problem is, if the validations fails, the .is-invalid
class is not applied in the input html tag. I think because @error('title.{{$lang->isocode}}')
is not correct.
What I'm doing wrong?
CodePudding user response:
=>title field validation you can use in request method so remove this .{{$lang->isocode}} that is not correct.@error('title') print your error message.
@error('title')
<div >{{ $message }}</div>
@enderror
CodePudding user response:
i think the issue you did'nt provide index of error bag :
@foreach ($langs as $k=>$lang)
<input
type="text"
name="title[{{$lang->isocode}}]"
id="title[{{$lang->idioma->codi}}]"
value="{{ $lang->title }}"
>
@endforeach