Laravel 5.3 and Parsley remote validation

402 views Asked by At

i tryed to remote validate the email and username for a registration form with parsley. I get allways the message:

public/checkUserName?username=tester 405 (Method Not Allowed)

So here is my code:

Input:

<input data-parsley-remote="{{ route('checkUserName') }}"  data-parsley-remote-message="Der Username ist bereits vergeben!" data-parsley-remote-options='{ "type": "POST", "dataType": "jsonp", "data": {_token: CSRF_TOKEN, "UserName": "username" } }'

Route:

Route::post('/checkUserName', [
   'uses' => 'UserController@checkUserName',
   'as' => 'checkUserName'
]);

UserController:

public function checkUserName(Request $request)
{        
    if(user::where('UserName','=',$request->input('UserName'))->exists()){
           return response::json('exists', 404);
        }else{
           return response::json('not exists', 200);
        }
}
1

There are 1 answers

0
The Alpha On

The error 405 (Method Not Allowed) is thrown by Laravel because request method GET is not allowed to handle the route registered for POST.

First of all, I'm not familiar with Parsley but after observing your code, what I've found is that, you have setup the parsley to send AJAX request using POST method but the data type is set to jsonp.

// Extracted from the question
data-parsley-remote-options='{
    "type": "POST",
    "dataType": "jsonp",
    "data": {_token: CSRF_TOKEN, "UserName": "username" }
}'

In this case, jsonp is not possible using POST method because it sends a GET request (using a script tag...) and hence, your route is not found because you've registered the route as POST.

So, it's happening because of inappropriate data type (jsonp) with the request method POST. I'm sure that, the Parsely is sending a GET request instead of POST request because of dataType:jsonp.

So, you can check the network tab in your browser inspector to find out the request method that is being sent and you should either register the route with Route::get() or change the dataType to something else, could be json but you should check the Parsely documentation for available data types.