3 回答

TA貢獻1886條經驗 獲得超2個贊
Yii 的活動表單 JS 將一些信息保存到yiiActiveForm數據屬性中。您可以使用某些submitting屬性來確定表單在提交之前是否處于驗證狀態。
$('.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貢獻1797條經驗 獲得超6個贊
您應該在 afterValidate 之后找到 has-error 類并顯示彈出窗口嘗試以下代碼
$('.validate-form').on('afterValidate', function (event, messages, errorAttributes) {
if ($('.validate-form').find('.has-error').length) {
$('.error-popup').show();
}
});

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