jquery - 在“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>
我没有得到如何在模态结构中显示错误。如果有人有想法,请帮助我。
解决方案
尝试取消注释:
// if ($validate->fails())
// {
// return response()->json(['errors'=>$validate->errors()->all()]);
// }
此代码以 json 格式返回错误并让您轻松打印它们
并在 jquery 中尝试记录 data.response 因为错误返回许多对象,如状态代码和其他东西
推荐阅读
- python - 为什么即使它在范围内,我也会收到索引超出范围错误消息?
- powershell - 为什么我不能使用带有 set-content 的 value 参数的脚本块?(延迟绑定)
- sap-iq - PowerDesigner 无法从物理数据模型生成数据库
- python - 使用 boto3 列出实例使用的密钥对
- php - 如何解析字符串以通过 ADB 发送 SMS 文本消息
- python - 无法读取 sqlite 数据并将其与 python 中的字符串进行比较
- google-maps-api-3 - 如何让我的 Google 地图显示在 .NetCore Web 应用程序中?
- r - 检查另一列/行的条目后将字符更改为日期格式
- r - 在 R 中使用 Variable() 时有没有办法施加整数约束
- java - 如何根据定义的大小将数据框中的一列拆分为多列