尝试在axios中显示Laravel验证错误

时间:2020-01-20 18:49:26

标签: jquery laravel axios

我正在尝试使用axios从我的应用程序中的laravel验证中获取错误,但它一直显示此错误。控制器会验证用户以及未填写的任何字段。它将返回一个错误,该错误将使用axios显示在页面上。

错误:

VM12049:87 Uncaught (in promise) TypeError: Cannot read property '0' of undefined
at <anonymous>:87:80
(anonymous) @ VM12049:87
XMLHttpRequest.send (async)
(anonymous) @ VM12045:8
e.exports @ VM12045:8
e.exports @ VM12045:8
Promise.then (async)
r.request @ VM12045:8
r.<computed> @ VM12045:8
(anonymous) @ VM12045:2
updateProfile @ VM12049:68
(anonymous) @ VM12049:32
dispatch @ VM12044:2
v.handle @ VM12044:2

我的代码

axios代码

  axios.post('/update-student-info', data, {
        "x-csrf-token": $("[name=_token]").val(),
        'content-type': 'multipart/form-data'
    })
        .then(response => {
            if (response.status === 200) {
                Swal.fire({
                    title: 'Information Updated',
                    text: '',
                    icon: 'success',
                    confirmButtonText: 'continue',
                    allowOutsideClick: false
                })
                $("#profile").val("");
                $("#update").text("Update");

            }
        }).catch(error => {

        first_name_error.text(error.response.data.errors.first_name[0]);
        last_name_error.text(error.response.data.errors.last_name[0]);
        phone_error.text(error.response.data.errors.phone[0]);
        fac_id_error.text(error.response.data.errors.fac_id[0]);
        dept_id_error.text(error.response.data.errors.dept_id[0]);
        console.log(error.response.data.errors);

    }).finally(() => {
        $("#profile_updates").html('Update');

    });

控制器代码

    public function updateStudentInfo(Request $request){
    $request->validate(
         [
            'first_name' => 'required|string|max:50',
            'last_name' => 'required|string|max:50',
            'phone' => 'required',
            'fac_id' => 'required',
            'dept_id' => 'required'
        ],[
        'fac_id.required' => 'This Field is required',
        'dept_id.required' => 'This Field is required'
    ]);

    try {

            OtherInstitutionStudents::where('email', Auth::user()->email)->update([
                'first_name' => $request->get('first_name'),
                'last_name' => $request->get('last_name'),
                'phone' => $request->get('phone'),
                'institution_id' => $request->get('fac_id'),
                'institution_level' => $request->get('dept_id')
            ]);

    } catch (ValidationException $e) {
        return response()->json([
            'status' => 'error',
            'msg' => 'error',
            'errors' => $e->errors()
        ], 422);
    }
    return response()->json($request->all());

}

我该怎么办?。

1 个答案:

答案 0 :(得分:3)

您假设每个字段都会有一个错误,可能并非如此。所以这段代码:

first_name_error.text(error.response.data.errors.first_name[0]);
last_name_error.text(error.response.data.errors.last_name[0]);
phone_error.text(error.response.data.errors.phone[0]);
fac_id_error.text(error.response.data.errors.fac_id[0]);
dept_id_error.text(error.response.data.errors.dept_id[0]);

应该修改为:

if (error.response.data.errors.first_name) {
   first_name_error.text(error.response.data.errors.first_name[0]);
}
if (error.response.data.errors.last_name) {
   last_name_error.text(error.response.data.errors.last_name[0]);
}
if (error.response.data.errors.phone) {
   phone_error.text(error.response.data.errors.phone[0]);
}
if (error.response.data.errors.fac_id) {
   fac_id_error.text(error.response.data.errors.fac_id[0]);
}
if (error.response.data.errors.dept_id) {
   dept_id_error.text(error.response.data.errors.dept_id[0]);
}