Как принять ошибки валидации с сервера, для дальнейшей работы с ними на клиенте?

Есть код:


Laravel:

function signup(Request $req)
    {
        $validation = Validator::make($req->all(),
    [
        'name'=>'required',
        'surname'=>'required',
        'email'=>'required|unique:users|email',
        'password'=>'required',
    ]);
    if($validation->fails())
    {
        return response()->json([
            'code'=>422,
            'content'=>$validation->errors()
        ],422);
    }
    User::create($req->all());
    return response()->json(['code'=>200])->setStatusCode(200);
    }



JavaScript:

 registration() {
            $.ajax({
                type: "post",
                url: "http://127.0.0.1:8000/api/signup",
                data: {
                    name: this.registrationForm.name,
                    surname: this.registrationForm.surname,
                    email: this.registrationForm.email,
                    password: this.registrationForm.password,
                },
                success: function (data) { app.showSuccess('Вы успешно зарегестрировались') },
                error: function (jqXHR, textStatus, errorThrown) { console.log(jqXHR); console.log(textStatus); console.log(errorThrown) },
            });
            return false;
        }

Суть вопроса:

Сервер возвращает ошибку, а мне нужны данные о валидации... т.е сервер должен вернуть

{
            'code':422,
            'content'=>(тут дожны быть ошибки)
}

Я думаю, проблема в 422 коде http.

Заранее спасибо всем!


Ответы (0 шт):