I use FormRequest in my app for validating data.
Example code:
<?php
namespace App\Http\Requests;
use Illuminate\Foundation\Http\FormRequest;
class UserRequest extends FormRequest
{
/**
* Determine if the user is authorized to make this request.
*
* @return bool
*/
public function authorize()
{
return true;
}
/**
* Get the validation rules that apply to the request.
*
* @return array
*/
public function rules()
{
return [
'skills' => "required|array",
"skills.*" => "required|min:2|max:20"
];
}
}
Here by default when I pass to request this example data:
{
"skills" => [
"a",
"apple"
]
}
Then get error message:
{
"errors": {
"skills.0": [
"The skills.0 must be at least 2 characters."
],
"skills.1": [
"The skills.1 may not be greater than 4 characters."
]
}
}
How I can customize this validation error messages and get something like this errors on result:
{
"errors": {
"skills": [
"The skills with key 0 must be at least 2 characters.",
"The skills with key 1 may not be greater than 4 characters."
]
}
}