skip to Main Content

How can I totally eliminate this 422 (Unprocessable Content) error that is shown in the console? It only happens when validation fails like for instance, the image uploaded exceeds the file size that I required in validation.

Here’s my validation code.

$request->validate([
            'message' => 'required|max:5000',
            'id' => 'required|integer|exists:users,id',
            'content' => 'nullable|integer|exists:contents,id',
            'photo' => 'nullable|image|mimes:jpeg,png,jpg|max:2048'
        ], [
            'message.required' => ERR_COMMENT_REQUIRED,
            'message.max' => ERR_PHOTO_MAX,
            'id.required' => ERR_ID_REQUIRED,
            'id.integer' => ERR_ID_INTEGER,
            'id.exists' => ERR_ID_EXIST,
            'photo.image' => ERR_PHOTO_IMAGE,
            'photo.mimes' => ERR_PHOTO_JPG_PNG_GIF,
            'photo.max' => ERR_PHOTO_MAX
        ]); 

In my ajax code, I already created an error handler to catch the 422 error and inform the user of what’s going on. It’s perfectly working so far. I just could not figure out how to totally remove the 422 (Unprocessable Content) error in the console window or what is causing it? I’ve been scratching my head all night lol. Hope someone can help me figure out how to solve this. Thanks.

2

Answers


  1. it is not possible. Because it is from browser.
    However you can clear the console in error block. But it is not recommended

    Login or Signup to reply.
  2. Laravel validator will return 422 status code on validation fails. You can create validation function as below to return custom status code.

     $validator = Validator::make($request->all(), [
            'first_name' => 'required',
            'last_name' => 'required',
            'email' => 'required',
            'message' => 'required',
        ]);
    
        if($validator->fails()){
            $errors = collect($validator->errors());
            $error = $errors->unique()->first();
            return return Response::json([
               'success' => false,
               'message' => $error[0]
            ], 200); //custom status code
      }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search