Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

Brand12's avatar

Changing Laravel Form Validation Status to 200 from 422

I am working on a laravel project where i am mkaing use of Jquery Ajax to validate the form which is returning a 422 status but i need a 200 status, don't know if there is a workaround to do, below are the code i used.

the route function where i am validating the form

use Illuminate\Http\Request;
use Illuminate\Support\Facades\Route;
use App\Http\Requests;

Route::get('/user', function (Request $request) {
    return ["username" => "brand", "sex" => "M"];
});

Route::post('/tourist_login', function (Requests\LoginFormRequest $request) {
    $request->validated();

    return $request;
});

The jQuery ajax function

$("#loginForm").on("submit", async function (e) {
    e.preventDefault();
    let fields = new FormData(this);
    try {
        let login = await $.ajax({
            method: "POST",
            url: "/api/tourist_login",
            data: fields,
            dataType: "JSON",
            cache: false,
            contentType: false,
            processData: false,
        });

        console.log(login.errors);
    } catch (error) {
        console.log(error);
    }
});
0 likes
13 replies
martinbean's avatar

I am working on a laravel project where i am mkaing use of Jquery Ajax to validate the form which is returning a 422 status but i need a 200 status

@brand12 No, you really don’t 😬

200 means "OK". The response was not “OK” if there are validation errors.

How are on earth are you intending to distinguish between successful responses and error responses if it just returns 200 OK every time?

Laravel returns a 422 status for validation errors because it’s a good convention to do so. So handle requests properly instead of trying to override good conventions with absolutely terrible ones.

Execute your request. Change how you handle the response based on the status code:

$('#loginform').on('submit', function (event) {
    event.preventDefault();
    
    $.ajax({
        data: new FormData(event.target),
        dataType: 'json',
        error: function (xhr) {
            // Login request errored, so actually handle errors
            // i.e. display validation messages next to inputs
            $.each(xhr.response.errors, function (field, messages) {
                // Display messages next to field
            });
        },
        method: 'POST',
        success: function (data) {
            // Login request was successful
            // Do whatever
        },
        url: '/api/tourist_login',
    });
});

Also, you don’t “log in” to RESTful APIs.

1 like
Brand12's avatar

@martinbean thanks for that. What name can i give the route, i just started the world of routing and Laravel of a thing, apologies if I have lots of spaghetti code written there.

I am wanting to ask, in the console of my browser, it's flagging some error about the 422 status, is it normal.

Snapey's avatar

@Brand12 Regarding your route Route::post('/tourist_login use of underscore in URLs is generally frowned on

1 like
Snapey's avatar

@Brand12 no, stick with lowercase and hyphens, eg tourist-login or just touristlogin

1 like
martinbean's avatar

@Brand12 The route name isn’t the problem. Why are you intercepting a form submission, to do a “login” via AJAX in the first place? Why aren’t you just submitting to a /login route to log a user in? Why are you doing it via AJAX, and to a route prefixed with “/api”?

Brand12's avatar

@martinbean ohh, just trying something, the thing i wanted to do is that i don't want the page to reload because the form is in a modal.

martinbean's avatar

@Brand12 The page won’t reload if you make the request using AJAX, as you’ll receive a JSON response with a 422 status code and the validation errors in the response body.

Snapey's avatar

what happened to your original question, at which I also gave the same comment

1 like
Brand12's avatar

@Snapey when I posted it and saw that the codes aren't pretty, was looking for how to make it pretty so, i deleted it to not let it be seen, just joined the community today kind of new to the ecosystem, didn't know seen and replied, apologies.

jaseofspades88's avatar

It's almost as if there's a correlation between, a Level 1, non-subscribing member of this forum and threads, which tend to start with a simple question, get the same response from about six pillars of the community, answering the question...

1 like
jlrdw's avatar

Also see if this video helps https://www.youtube.com/watch?v=Ql5z9TjXWLY

It is just a basic sanctum video, but read the sanctum chapter also. The video just explains the issuing of the token.

Edit:

I also suggest taking the free laravel from scratch training course.

And the free php course if new to programming.

1 like

Please or to participate in this conversation.