【发布时间】:2021-02-17 20:37:00
【问题描述】:
我正在使用 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>
我不知道如何在模态结构中显示错误。如果有人有想法,请帮助我。
【问题讨论】:
-
ajax 验证将帮助您做到这一点itsolutionstuff.com/post/…
-
有很多方法可以在前端检查验证,html 有一个
required属性可以做到这一点:<input type="text" name="name" required>
标签: jquery ajax laravel validation laravel-7