[英]Laravel JsValidation is not working with Ajax submitted form
I'm using Laravel JsValidation
, JsValidation
working properly with other forms, but because I should submit the form with Ajax, JsValidation is not working properly, and form submitted. 我正在使用
Laravel JsValidation
, JsValidation
与其他表单一起正常工作,但因为我应该使用Ajax提交表单,JsValidation无法正常工作,并且表单已提交。
$validator = JsValidation::make($rules);
and in view 在视野中
{!!$validator!!}
Since the validation call is asynchronous, mark that as synchronous by doing: 由于验证调用是异步的,因此请执行以下操作将其标记为同步:
async: false
[optional] and to prevent form submit default functionality, do: [可选]并为防止表单提交默认功能,请执行以下操作:
e.preventDefault();
on submit button click. 在提交按钮单击。 After doing validation you'll get response based on that response, you can allow form to submit or not.
在进行验证后,您将根据该响应获得响应,您可以允许表单提交或不提交。
Simple Code Snippet:
简单代码片段:
$("#form-submit").on('submit', function(){
var errorMsg = 'Please provide your correct information.';
$(".errorMsg ").text("");
var txtName = $("input#txtName").val();
var responseStatus = false;
var userData = {
'name' : txtName
};
$.ajax({
type: 'POST',
url: '/validate-data',
async: false,
data: userData,
success: function(result){
if(result.status === true){
responseStatus = true;
} else {
responseStatus = false;
errorMsg = result.message;
$(".errorMsg ").text(errorMsg);
return false;
}
},
error: function (xhr, ajaxOptions, thrownError) {
alert('Oops! Something went wrong. Please try again.'); return false;
}
});
if(responseStatus === false){
return false;
} else {
return true;
}
});
See, if that helps. 看,如果这有帮助。
Try to use this snippet: 尝试使用此代码段:
$('form').on('submit', function() {
if($(this).valid()) {
// do your ajax stuff here
}
return false;
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.