修改 yii.activeForm 是个好主意吗.js如果不是,另一种方法是什么

Is it a good idea to modify yii.activeForm.js if not what is the alternative way?

本文关键字:如果不 另一种 js 方法 是什么 yii activeForm 好主意 修改      更新时间:2023-09-26

yii.activeForm.js 帮助客户端验证并允许或拒绝表单提交。点击时,活动表单会自动调用 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']) ?>

但是我觉得修改作曲家生成的文件不是一个好习惯。

修改 yii.activeForm .js 可以吗?如果不是,我如何在不接触 yii.activeForm 的情况下实现相同的.js.?

我还注意到 web/asset 目录有一些随机命名的子目录,并且在两个项目(安装)上并不相同。
例如:web/assets/e67bec0b/yii.activeForm.js
是出于某种安全目的吗?

谢谢。

您可以使用 yiiActiveForm 的event轻松强制更改。例如,在您的情况下,您可以执行以下操作:

$(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...
  }
});
相关文章: