简体   繁体   English

yii2 ajax 验证表格

[英]yii2 ajax validation tabular

I have a form master detail for tabular input and with enabledAjaxValidation=true我有一个用于表格输入和 enabledAjaxValidation=true 的表单主详细信息

So far so good, the form validates all the rules and shows the error messages when submitting or changing any value of any control (onchange event).到目前为止一切顺利,表单验证所有规则并在提交或更改任何控件的任何值(onchange 事件)时显示错误消息。 The problem comes when I add controls to the form using ajax, the latter do not behave like the original ones, they do not show the error messages.当我使用 ajax 向表单添加控件时出现问题,后者的行为与原始控件不同,它们不显示错误消息。

在此处输入图片说明

The same when do submit with button I think that与按钮提交时相同我认为

You need to add the newly created/added field to the validation manually for any dynamically created inputs using the yiiActiveForm.add() function.您需要使用yiiActiveForm.add()函数为任何动态创建的输入手动添加新创建/添加的字段到验证中。

You havent added the code you are currently using when you click on the button and add a new input to the form via ajax.当您单击按钮并通过 ajax 向表单添加新输入时,您还没有添加当前使用的代码。 So what you need to do is when you receive the response and append the input to the form just add the new input using the following code.因此,您需要做的是当您收到响应并将输入附加到表单时,只需使用以下代码添加新输入即可。

Note: Change the form and field attributes accordingly注意:相应地更改表单和字段属性

$('#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"});
    }
});

Read more about the activeform valiadation js阅读有关activeform 验证 js 的更多信息

Update更新

If you dont wish to add the validation function manually for every input and you have tabular inputs you can access any of the already created similar field and bind the validation function from it.如果您不想为每个输入手动添加验证函数并且您有表格输入,您可以访问任何已经创建的类似字段并从中绑定验证函数。

For instance in the above example if the the name field is tabular and belongs to the model Contact and you already have a name field populated in the form #contact-0-name you can use the yiActiveForm.find() function to access the attributes of that field and assign the existing validation.例如,在上面的示例中,如果name字段是表格并且属于模型Contact并且您已经在表单#contact-0-name填充了名称字段,则您可以使用yiActiveForm.find()函数来访问属性该字段并分配现有的验证。 see an example below看下面的例子

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
});

use somthing like below code使用类似下面的代码

 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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM