I've custom validation code:
$this->validate($request, [
'array' => 'required|array|max:100',
'array.*' => 'required|string|distinct|min:3'
], [
'array.max' => 'Array can't have more :max items',
]);
Here how I can add cusom message with array items rule?
For example: 'array.item.min' => 'Array items length can't be greater :min charackters'
Example laravel default validation error message for array items:
{
message: "The given data was invalid."
}
errors: {
array.3: ["The array.3 must be at least 3 characters."]
}
array.3: ["The array.3 must be at least 3 characters."]
0: "The array.3 must be at least 3 characters."
message: "The given data was invalid."
How I can replace this validation message with my single message for array items?
You can try adding custom messages on each element when the form is submitted. Here's the sample code
$customMessages['array.max'] = "Array can't have more :max items";
foreach ($request->get('array') as $key => $value) {
$customMessages['array.' . $key . '.min'] = "Array items length can't be greater :min characters";
}
$this->validate($request, [
'array' => 'required|array|max:100',
'array.*' => 'required|string|distinct|min:3'
], $customMessages);
If you want to show the error in your view, you can do this:
@if ($errors->has('array.0'))
{{ $errors->first('array.0') }}
@endif
If you have foreach in the view
@if ($errors->has('array.'.$index))
{{ $errors->first('array.'.$index) }}
@endif