[英]Laravel, return json error on duplicated inputs
How can I show message on error duplicates?如何显示有关错误重复项的消息? the form does not complete the create process because of duplicate but its just freezing without showing any message, but if I left the fields empty then it shows that those are required由于重复,表单没有完成创建过程,但它只是冻结而不显示任何消息,但如果我将字段留空,那么它表明这些是必需的
public function store(Request $request)
{
$validator = \Validator::make($request->all(), [
'name'=>'required|unique:countires',
'code'=>'required|unique:countires'
]);
if ($validator->fails()) {
return response()->json(['errors' => $validator->errors()->all()]);
}
$this->SourceData->storeData($request->all());
return response()->json(['success'=>'Added successfully']);
}
ajax阿贾克斯
$.ajax({
url: $('#archiveurl').attr('href'),
method: 'post',
data: $("#createform").serialize(),
beforeSend: function( xhr ) {
Command: toastr["info"]("Uploading Data ...", "Sending Request");
},
success: function(result) {
toastr.clear();
if(result.errors) {
$('.alert-danger').html('');
$.each(result.errors, function(key, value) {
$('.alert-danger').show();
$('.alert-danger').append('<strong><li>'+value+'</li></strong>');
});
} else {
$('.alert-danger').hide();
$('.alert-success').show();
Command: toastr ["success"] ("Added successfully", "Added status",{ timeOut: 900 });
$('.clear_this input[type="text"]').val('');
$('.datatable').DataTable().ajax.reload();
}
public function store(Request $request)
{
$validator = Validator::make($request->all(), [
'name' => 'required|unique:countries,name',
'code' => 'required|unique:countries,code',
]);
if ($validator->fails()) {
$errors = $validator->errors();
return response()->json(['status' => false, 'errors' => $errors]);
}
$this->SourceData->storeData($request->all());
return response()->json(['success'=>'Added successfully']);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.