[英]yii2 ajax validation tabular
您需要使用yiiActiveForm.add()
函數為任何動態創建的輸入手動添加新創建/添加的字段到驗證中。
當您單擊按鈕並通過 ajax 向表單添加新輸入時,您還沒有添加當前使用的代碼。 因此,您需要做的是當您收到響應並將輸入附加到表單時,只需使用以下代碼添加新輸入即可。
注意:相應地更改表單和字段屬性
$('#form-id').yiiActiveForm('add', {
id: 'input-id',
name: 'input-name',
container: '.field-input',
input: '#input-id',
error: '.help-block',
validate: function (attribute, value, messages, deferred, $form) {
yii.validation.required(value, messages, {message: "Validation Message Here"});
}
});
閱讀有關activeform 驗證 js 的更多信息
更新
如果您不想為每個輸入手動添加驗證函數並且您有表格輸入,您可以訪問任何已經創建的類似字段並從中綁定驗證函數。
例如,在上面的示例中,如果name
字段是表格並且屬於模型Contact
並且您已經在表單#contact-0-name
填充了名稱字段,則您可以使用yiActiveForm.find()
函數來訪問屬性該字段並分配現有的驗證。 看下面的例子
var fieldAttributes = $("#form-id").yiiActiveForm("find", 'contact-0-name');
$('#form-id').yiiActiveForm('add', {
id: 'contact-1-name',
name: '[1][name]',
container: '.field-name',
input: '#contact-1-name',
error: '.help-block',
validate: fieldAttributes.validate
});
使用類似下面的代碼
error: function(jqXHR,textStatus,errorThrown) { stopLoader('.modal-content'); $('.csv_errors').show(); if(jqXHR.status==422){ var responseText = $.parseJSON(jqXHR.responseText); $.each(responseText.errors,function(key,value){ $('.csv_error ul').append('<li>'+value+'</li>'); }); }else{ var responseText = $.parseJSON(jqXHR.responseText); $('.csv_error ul').append('<li>'+responseText.message+'</li>'); } }
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.