首页 > 解决方案 > 在“422(无法处理的实体)”Laravel7 中验证错误时出现错误

问题描述

我正在使用 laravel 7 。当我输入成功添加到数据库的所有数据时。但是,如果任何字段为空,则会出现错误““422(不可处理实体)”Laravel7

如果出现任何 laravel 验证错误,那么我想显示到相应的字段错误中。但我没有得到价值

error: function (data) {
   console.log("data");
      console.log('Error:', data);
      $('#btn-save').html('Save Changes');
}

下面是我的编码部分

  if ($("#productForm").length > 0) {
            $("#productForm").validate({
  
                submitHandler: function(form)
                 {
  
                      var actionType = $('#btn-save').val();
                      $('#btn-save').html('Sending..');
                       
                     $.ajax({
                              data: $('#productForm').serialize(),
                              
                              url:"contact-list/store",
                              type: "POST",
                              dataType: 'json',
                              success: function (data) {
                                console.log("dgfd");
                              $('#productForm').trigger("reset");
                              $('#user_table').DataTable().ajax.reload();
                              $('#ajax-product-modal').modal('hide');
                              $('#btn-save').html('Save Changes');
                              var oTable = $('#laravel_datatable').dataTable();
                              oTable.fnDraw(false);
               
                              },
                              error: function (data) {
                                console.log("data");
                                  console.log('Error:', data);
                                  $('#btn-save').html('Save Changes');
                              }
                            });
                    }
            })
        }

我的控制器:

    public function store(Request $request)
    {
        $validate = $request->validate([
                        'name' => 'required',
                        'email' => 'email',
                        'phone' => 'digits:10',
                        'address' => 'required',
                        'country' => 'required',
                        'state' => 'required',
                        // 'comment' => 'required',
                        'organization' => 'required',
                        'captcha' => 'required|captcha'
                    ],
                    [
                        'captcha.captcha' => 'Incorrect Captcha'
                    ]
                );

        // if ($validate->fails())
        // {
        //     return response()->json(['errors'=>$validate->errors()->all()]);
        // }

        $id=$request->contact_id;

        $customer = CustomerContact::find($id);
        $customer->name = $request->name;
        $customer->email = $request->email;
        $customer->phone = $request->phone;
        $customer->address = $request->address;
        $customer->country_id = $request->country;
        $customer->state_id = $request->state;
        $customer->comment = $request->comment;
        $customer->organization = $request->organization;
        $customer->captcha = $request->captcha;

 
        $a=$customer->update();
        return Response::json($a);

    }

Nd modal 
 
    <div class="modal fade" id="ajax-product-modal" aria-hidden="true">
<div class="modal-dialog">
<div class="modal-content">
    <div class="modal-header">
        <h4 class="modal-title" id="productCrudModal"></h4>
    </div>
    <div class="modal-body">
        
        <form id="productForm" name="productForm" class="form-horizontal">
           
            @csrf
             <div class="form-header">
                <h4>Edit Employee Information</h4>
             </div>

            <div class="form-group">
                <label class="required">Your Name</label>
                <input type="hidden" name="contact_id" id="contact_id">
                <input type="text" placeholder="Your Name" class="form-control" name="name" id="name">
                    <span class="error_message"></span>
            </div>

            <div class="form-group">
                <label>Email Id</label>
                <input type="text" placeholder="Email Id" class="form-control" name="email" id="email">
                    <span class="error_message"></span>
            </div>
   </div>
  </div>
  </div>

在此处输入图像描述

我没有得到如何在模态结构中显示错误。如果有人有想法,请帮助我。

标签: jqueryajaxlaravelvalidationlaravel-7

解决方案


尝试取消注释:

// if ($validate->fails())
    // {
    //     return response()->json(['errors'=>$validate->errors()->all()]);
    // }

此代码以 json 格式返回错误并让您轻松打印它们

并在 jquery 中尝试记录 data.response 因为错误返回许多对象,如状态代码和其他东西


推荐阅读