yii.activeForm.js 帮助客户端验证并允许或拒绝表单提交。 Onclick时,事件表单自动调用submitForm: function()。
现在,我想在验证期间将事件表单按钮文本更改为“正在处理”,并在成功验证后禁用该按钮以防止双击。
我稍微修改了(手动包含 4 行)yii.activeForm.js 文件,它按照我需要的方式运行良好。
submitForm: function () {
var oldtext= $('#smartbtn').text(); // manually included
$('#smartbtn').text('Processing...'); // manually included
var $form = $(this),
data = $form.data('yiiActiveForm');
if (data.validated) {
// Second submit's call (from validate/updateInputs)
data.submitting = false;
var event = $.Event(events.beforeSubmit);
$form.trigger(event);
if (event.result === false) {
data.validated = false;
submitFinalize($form);
return false;
}
updateHiddenButton($form);
$('#smartbtn').attr('disabled', 'disabled');// manually included
return true; // continue submitting the form since validation passes
} else {
$('#smartbtn').text(oldtext); // manually included
// First submit's call (from yii.js/handleAction) - execute validating
setSubmitFinalizeDefer($form);
if (data.settings.timer !== undefined) {
clearTimeout(data.settings.timer);
}
data.submitting = true;
methods.validate.call($form);
return false;
}
},
// active form
<?= Html::submitButton($model->isNewRecord ? Yii::t('app', 'Create') : Yii::t('app', 'Update'), ['class' => $model->isNewRecord ? 'btn btn-success' : 'btn btn-primary'
,'id'=>'smartbtn']) ?>
但是我觉得修改 Composer 生成的文件不是一个好习惯。
修改 yii.activeForm.js 可以吗? 如果不是,我如何在不接触 yii.activeForm.js 的情况下实现相同的功能?
我还注意到 web/asset 目录有一些随机命名的子目录,并且在两个项目(安装)上并不相同。
例如:web/assets/e67bec0b/yii.activeForm.js
是出于某种安全目的吗?
谢谢。
最佳答案
您可以使用 yiiActiveForm 的事件
轻松强制进行更改。例如,在您的情况下,您可以执行以下操作:
$(document).on({
beforeValidate: function (event, messages, deferreds) {
// Disable button or change text with find child of event variable.
},
afterValidate: function (event, messages, errorAttributes) {
// Restore texts...
}
});
关于javascript - 修改 yii.activeForm.js 是个好主意吗?如果不是,替代方法是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35861429/