How can i do a validation on a array?
here is the validation code:
$validation = Validator::make($request->input(), [
'title' => 'required|max:255'
]);
here is the validation check:
if ($validation->fails())
{
return redirect()->back()->withErrors($validation->errors());
}
And this is the array it needs to validate:
array:1 [▼
1 => ""
]
Note that the key 1 here is needed for multi language.
Im looking forward to a response :)
In order for validation to work you need validation rules keys to match keys in the array that contains data you want to validate. Your validation rules define field called title, yet I don't see a title field in your data array.
If such array contains subarrays that contain fields like aforementioned title that share common structure and validation rules, they way to validate that is to iterate through the base array and to call Validator::make()->fails() on each of the subarrays:
foreach ($request->input() as $subarray) {
$validation = Validator::make($subarray, [
'title' => 'required|max:255'
]);
if ($validation->fails())
{
return redirect()->back()->withErrors($validation->errors());
}
}
validation->each('title', ['required', 'max:255']);
was the solution here