Reputation: 1485
I've got basic laravel validator for my request:
$validator = Validator::make($request->all(), [
'speciality' => 'required|array|min:1',
]);
if ($validator->fails()) {
return response([
'status' => 'error',
'error' => 'invalid.credentials',
'message' => 'error validation'
], 400);
}
speciality should be an array, so I define it as required|array|min:1
The problem is when my request has an empty array with null inside: speciality: [ null ]
and this somehow passes my validator.
How can I correct it to catch this case?
Upvotes: 1
Views: 2909
Reputation: 7334
Using dot to access the underlying elements/values:
$validator = Validator::make($request->all(), [
'speciality' => 'required|array|min:1',
'speciality.*' => 'required' //ensures its not null
]);
See the Validation: Validating Arrays section of the documentation.
Notice that [null]
in not an empty
array, that's why your initial validation was passed. You can verify this doing:
dd(empty([null]));
This will output:
false
Upvotes: 6