0

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?

2 Answers 2

1

You request must have the Accept header set to application/json.

Once it's done, every responses from Laravel will be in json.

Sign up to request clarification or add additional context in comments.

Comments

1

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()]);
        }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.