Reputation: 11
I have this validation code that check if my input is a valid image with max size of 2048
:
$request->validate([
'file' => 'required|image|mimes:jpeg,png,jpg,gif,svg|max:2048',
]);
If the validation doesn't pass, I'd like to return a response in JSON format, how could I do that?
Upvotes: 0
Views: 445
Reputation: 153
Just do this
$rules = ['file' => 'required|image|mimes:jpeg,png,jpg,gif,svg|max:2048'] ;
$validator = Validator::make($request->file , $rules) ;
if ($validator->fails()) {
return response()->json(['message' => 'there is been an error', 'error message' => $validator->errors()]);
}
Upvotes: 1
Reputation: 3847
You request must have the Accept
header set to application/json
.
Once it's done, every responses from Laravel will be in json.
Upvotes: 1