Jquery 在“验证”中出错时获取错误;422(不可加工实体)“;拉维17

Jquery 在“验证”中出错时获取错误;422(不可加工实体)“;拉维17,jquery,ajax,laravel,laravel-7,Jquery,Ajax,Laravel,Laravel 7,我使用的是Laravel7。更新数据时出现问题。当我编辑并更新数据时,其已成功更新,但出现验证错误,然后控制台“422(不可处理实体)”Laravel7中出现错误。我不知道为什么会这样。 我的脚本代码如下 if ($("#productForm").length > 0) { $("#productForm").validate({ submitHandler: function(

我使用的是Laravel7。更新数据时出现问题。当我编辑并更新数据时,其已成功更新,但出现验证错误,然后控制台“422(不可处理实体)”Laravel7中出现错误。我不知道为什么会这样。 我的脚本代码如下

    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');
                              }
                            });
                    }
            })
        }

my controller :
   
    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>
if($(“#productForm”).length>0){
$(“#productForm”)。验证({
submitHandler:函数(表单)
{
var actionType=$('#btn save').val();
$('#btn save').html('Sending..');
$.ajax({
数据:$('#productForm')。序列化(),
url:“联系人列表/商店”,
类型:“POST”,
数据类型:“json”,
成功:功能(数据){
控制台日志(“dgfd”);
$('#productForm')。触发器(“重置”);
$('#user_table').DataTable().ajax.reload();
$(“#ajax产品模式”).model('hide');
$('#btn save').html('save Changes');
var oTable=$('#laravel_datatable')。datatable();
可旋转。fnDraw(假);
},
错误:函数(数据){
控制台日志(“数据”);
console.log('错误:',数据);
$('#btn save').html('save Changes');
}
});
}
})
}
我的控制器:
公共函数存储(请求$Request)
{
$validate=$request->validate([
“名称”=>“必需”,
“电子邮件”=>“电子邮件”,
“电话”=>“数字:10”,
'地址'=>'必需',
'国家'=>'必需',
'状态'=>'必需',
//'注释'=>'必需',
“组织”=>“必需”,
“验证码”=>“需要验证码”
],
[
“验证码。验证码”=>“验证码不正确”
]
);
//如果($validate->fails())
// {
//返回响应()->json(['errors'=>$validate->errors()->all()]);
// }
$id=$request->contact\u id;
$customer=CustomerContact::find($id);
$customer->name=$request->name;
$customer->email=$request->email;
$customer->phone=$request->phone;
$customer->address=$request->address;
$customer->country\u id=$request->country;
$customer->state\u id=$request->state;
$customer->comment=$request->comment;
$customer->organization=$request->organization;
$customer->captcha=$request->captcha;
$a=$customer->update();
返回响应::json($a);
}
Nd模态
@csrf
编辑员工信息
你的名字
电子邮件Id
请参阅控制台附件

作为响应,获取正确的验证消息

如果有人有主意,请帮我

data: $('#productForm').serialize(),
这是您发送到服务器的数据,我猜您的数据没有capcha值,所以现在我们有2个解决方案:

1:在验证控制器时,需要删除
captcha

2:向服务器发出请求时向数据添加验证码值


但我认为您应该选择选项1,因为验证码应该在客户端进行检查,不需要在服务器中进行检查

似乎验证失败您是否已经输入了验证码?输入时验证码似乎为空。验证失败,但ajax错误响应未在console.log(数据)中获得错误消息。否尚未放入验证码错误似乎暗示验证码字段没有使其生效我不想更新没有验证码值的数据?我想添加空验证和匹配验证码。好的,所以当你们提交时,你们会向用户显示错误,比如服务器返回是的。如果验证码(任何其他字段)为空,则显示错误“验证码不为空”