Javascript yii2中的js活动表单验证
我有一个活动表单,我正在尝试使用下面编写的脚本对其进行验证:Javascript yii2中的js活动表单验证,javascript,yii,yii2,active-form,Javascript,Yii,Yii2,Active Form,我有一个活动表单,我正在尝试使用下面编写的脚本对其进行验证: jQuery("#form").yiiActiveForm("submitForm"); 问题是,脚本总是返回false。但当我第二次延迟调用它时,它返回true jQuery("#form").yiiActiveForm("submitForm"); setTimeout(function () { if (jQuery("#form").yiiActiveForm("submitForm")) { //ajax
jQuery("#form").yiiActiveForm("submitForm");
问题是,脚本总是返回false。但当我第二次延迟调用它时,它返回true
jQuery("#form").yiiActiveForm("submitForm");
setTimeout(function () {
if (jQuery("#form").yiiActiveForm("submitForm")) {
//ajax call...
}
}, 300);
我的问题是,在yii2中验证是如何工作的?
有没有更好的方法来验证js中的活动表单
当我使用
jQuery("#form").yiiActiveForm("submitForm");
在调用提交处理程序之前
$('#form').on('beforeSubmit', function (e) {
alert('message');
});
beforeSubmit是否自动验证表单?Simple
你应该试试这个
<?php
$this->registerJs('
jQuery("#w0").yiiActiveForm("add",{
"id": "customer-name",
"name": "name",
"container": ".field-customer-name",
"input": "#customer-name",
"error": ".help-block.help-block-error",
"validate": function(attribute, value, messages, deferred, $form) {
yii.validation.required(value, messages, {
"message": "Name be blank bug."
});
yii.validation.string(value, messages, {
"message": "Name must be a string.",
"max": 255,
"tooLong": "Name should contain at most 255 characters.",
"skipOnEmpty": 1
});
}
});
');
?>
您必须考虑您的JavaScript代码放在哪里了?我创建了一个文件custom.js,并将所有js代码放在$(document.ready)(函数(){})中;可能重复的
w0 into your form ID
"id": "customer-name" into your input field ID
"container": ".field-customer-name" into input field div container class