Framework: Laravel 5.6 validator always returns 200 instead of 422 while ajax using.

Created on 22 May 2018  路  19Comments  路  Source: laravel/framework

I made a custom validator like in docs, but I always get a 200 ok even a validator throw errors. No 422, only 200 ok. How can I use ajax and validator for getting a response with error http code?

Most helpful comment

Late to the party, but in case someone else encounters this problem, you should make sure you're sending the request with the Accept: application/json header, otherwise Laravel >5.6 won't detect that it's an API request, and won't generate the 422 response.

All 19 comments

Please post the relevant parts of your code.

<?php

namespace App\Http\Requests;

use Illuminate\Foundation\Http\FormRequest;

class StoreQuestion extends FormRequest
{
    public function authorize()
    {
        return true;
    }

    public function rules()
    {
        return [
            'email' => 'required',
            'text' => 'required'
        ];
    }

    public function messages()
    {
        return [
            'email.required' => 'Email is empty!',
            'text.required'  => ':-( text is empty',
        ];
    }
}

and

<?php

namespace App\Http\Controllers;

use App\Http\Requests\StoreQuestion;

class QuestionController extends Controller
{
    public function index() {

    }

    public function store(StoreQuestion $request) {

        $user = Auth::user();

        return response()->json(['user' => $user], 200);
    }
}

Hi there,

Welcome to Laravel and we are glad to have you as part of the community.

Unfortunately this GitHub area is not a support area for general application issues. This is only for issues/bugs with the framework code itself.

Can I suggest closing your issue ticket here? Instead try asking your question on one of the many great community support areas that will likely give you a better answer more quickly:

  • Laravel Slack (https://larachat.co/)
  • Laravel.io Forum (https://laravel.io/forum)
  • Laracasts Forum (https://laracasts.com/discuss)
  • StackOverflow (http://stackoverflow.com/questions/tagged/laravel)

Thanks in advance.

issues/bugs with the framework code itself.

I thinks this is a bug. Because in 5.4 all works fine

It works for me. Does the invalid request reach return response()->json(['user' => $user], 200);?

I don't even get to this part of code.
I get an error message, but with status 200. two local machines - the same result

image
image

Validator works, if empty fields. But status code....always 200

I have the same problem. before cpanel updated everything work fine. but after update cpanel . my validation get 200 status to me

I also meet the same problem, I'm in stuck and can't find the solution. This is strange and very bad things for me because of the closest deadline. Anybody can help me please.

I also meet the same problem, I'm in stuck and can't find the solution. This is strange and very bad things for me because of the closest deadline. Anybody can help me please.

Hi! What laravel version do you use?

Hi, I use Laravel 5.5.45
It's always response with status 200 despite of validation error.

I also meet the same problem, I'm in stuck and can't find the solution. This is strange and very bad things for me because of the closest deadline. Anybody can help me please.

I change my validate message file and i work fine right now

Hi, I use Laravel 5.5.45
It's always response with status 200 despite of validation error.

Maybe you will write to me an email?

Late to the party, but in case someone else encounters this problem, you should make sure you're sending the request with the Accept: application/json header, otherwise Laravel >5.6 won't detect that it's an API request, and won't generate the 422 response.

Late to the party, but in case someone else encounters this problem, you should make sure you're sending the request with the Accept: application/json header, otherwise Laravel >5.6 won't detect that it's an API request, and won't generate the 422 response.

Thanks a lot it worked for me

Late to the party, but in case someone else encounters this problem, you should make sure you're sending the request with the Accept: application/json header, otherwise Laravel >5.6 won't detect that it's an API request, and won't generate the 422 response.

Thanks a lot. This worked for me too!

@iraklisg I have an issue which may be related to the fact that >5.6 won't detect it's an API request. What is causing this change? Could you point me to some documentation?

@avandermeer could you please provide more context related to your issue?

Was this page helpful?
0 / 5 - 0 ratings