3 回答

TA貢獻(xiàn)1886條經(jīng)驗(yàn) 獲得超2個(gè)贊
Yii 的活動(dòng)表單 JS 將一些信息保存到y(tǒng)iiActiveForm數(shù)據(jù)屬性中。您可以使用某些submitting屬性來確定表單在提交之前是否處于驗(yàn)證狀態(tài)。
$('.validate-form').on('afterValidate', function (event, messages, errorAttributes) {
let data = $('.validate-form').data('yiiActiveForm');
//check if we are in submission process and if there are any errors
if (data.submitting && errorAttributes.length > 0) {
$('.error-popup').show();
}
});

TA貢獻(xiàn)1797條經(jīng)驗(yàn) 獲得超6個(gè)贊
您應(yīng)該在 afterValidate 之后找到 has-error 類并顯示彈出窗口嘗試以下代碼
$('.validate-form').on('afterValidate', function (event, messages, errorAttributes) {
if ($('.validate-form').find('.has-error').length) {
$('.error-popup').show();
}
});

TA貢獻(xiàn)1895條經(jīng)驗(yàn) 獲得超7個(gè)贊
您可以通過以下方式解決此問題:
$("#register-form").on("beforeSubmit", function(event){
$(this).yiiActiveForm('validateAttribute', 'contactform-phone');
$(this).yiiActiveForm('validateAttribute', 'contactform-email');
if ($(this).find('.has-error').length) {
...
}
...
return false;
});
提交按鈕會(huì)觸發(fā)beforeSubmit事件,該事件又會(huì)觸發(fā)您需要的任何字段的驗(yàn)證。如果驗(yàn)證結(jié)果錯(cuò)誤,您可以執(zhí)行與顯示錯(cuò)誤消息相關(guān)的 JS 邏輯。
- 3 回答
- 0 關(guān)注
- 199 瀏覽
添加回答
舉報(bào)