Reco Jhonatan Reco Jhonatan - 6 months ago 164
Ajax Question

response ajax and validation response with laravel

I'm trying to make a register with ajax, validating data with laravel.
I realize sending, but not how to return the validation error and receive the error in the js code and display the error in a div.

I'm using version 5.2 of laravel. thanks the help :)

js:

function send(event){

event.preventDefault();

$.ajax({


type: 'post',
url: "{{route('NewUser')}}",
dataType: 'json',
data: {
name: $('#name').val(),
email: $('#email').val(),
password: $('#password').val(),
cpassword: $('#cpassword').val(),
_token: $('#new-user').attr('data-token')
},
success: function(errors){

$('#Register').modal('hide');

},
error: function(errors) {


}

});

}


laravel controller:

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Validator;
use App\Http\Controllers\Controller;
use App\Http\Requests;

class UserController extends Controller
{
public function index()
{
return view('home');
}
public function NewUser(Request $request)
{

$this->validate($request,[
'name' => 'required|max:255',
'email' => 'required|email|max:255|unique:users',
'password' => 'required|min:8|same:cpassword',
'cpassword'=> 'required|min:8'
]);

CreateNewUSer($request);

}

Protected function CreateNewUser(Request $request){

}

Answer

To reach your goal, I recommend you to use FormRequest
First of all create form request class

php artisan make:request UserRequest

Then add some rules in rules method of UserRequest

/**
 * Get the validation rules that apply to the request.
 *
 * @return array
 */
public function rules()
{
    return [
        'name' => 'required|max:255',
        'email' => 'required|email|max:255|unique:users',
        'password' => 'required|min:8|same:cpassword',
        'cpassword'=> 'required|min:8',
    ];
}

For simplicity all users can make this request(you should look over the docs, if you want check user permissions that way)

/**
 * Determine if the user is authorized to make this request.
 *
 * @return bool
 */
public function authorize()
{
    return true;
}

Further in your controller

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Validator;
use App\Http\Controllers\Controller;
use App\Http\Requests;

use App\Http\Requests\UserRequest;

class UserController extends Controller
{
    public function index()
    {
        return view('home');
    }
    public function NewUser(UserRequest $request)
    {
            // laravel will validate request itself automatically and
            // will go on if validate pass or will return back with 
            // status code 422, input and errors otherwise

            CreateNewUSer($request);

    }

    Protected function CreateNewUser(Request $request){

    }

Finally you can catch errors in you js scripts using jQuery's ajax .error()

function send(event){

    event.preventDefault();

    $.ajax({


        type: 'post',
        url: "{{route('NewUser')}}",
        dataType: 'json',
        data: {
            name: $('#name').val(),
            email: $('#email').val(),
            password: $('#password').val(),
            cpassword: $('#cpassword').val(),
            _token: $('#new-user').attr('data-token')
        },
        success: function(errors){

            $('#Register').modal('hide');

        },
        error: function(errors) {

            //handle errors here

        }

    });

}

EDIT put this in view before js with ajax function

<script>
    var userAjax = "{{ route('NewUser') }}";
</script>

and your ajax js

 $.ajax({

        type: 'post',
        url: userAjax,
        dataType: 'json',
        // ...