问题描述
在我的要求之一中,我形成了一个包含行和列的类似数组的网格表.一旦启动,我就进行服务呼叫并获取列和值以及错误消息.基于此,我为该列创建了一个表单数组,并且如果存在errormessage,则每个列都有errormessage,需要设置错误.
In one of my requirements, I have form an array-like grid table contains rows and columns. once on Init i am doing a service call and get the columns and values and error messages. based on that i have created a form array for that columns and each columns have errormessage if errormessage exists need to set errors.
我可以为这些列设置值,但在推入表单数组时无法设置错误,我也不知道如何推错误.停留在这条线上.
I can able to set value for those columns but not able to set errors while pushing into form array i don't know how could I push error as well. stuck on this line.
Qn?最初,我们如何根据服务响应错误消息在表单数组控件中设置错误我仅能看到最初设置值并为控件设置验证器的演示.
Qn?On initially how we could set the errors in form array controls based on service response error messagesi can able to see lots of demo only for set the value initially and set the validators for that controls.
但是在我的情况下,我需要设置错误并根据服务器响应设置一个值.
But in my case i need to set error and set a value basedon server response.
例如)的响应是
[
{fieldName:'productId',fieldValue:'123',errorMessage:'Product Id should be alphanumeric'},
{fieldName:'productname',fieldValue:'123',errorMessage:'Product name should be 10 characters'}],
等
onInit(){
//进行服务调用并获取值,一旦获取值将其推入formArray.但是如何也可以推入错误.
值运行正常
let result= service response;
result.forEach((item:any[], index) => {
let formData =this.formBuilder.group({
item.fieldName:[item.fieldValue],
item.fieldName:[item.fieldValue]
});
this.formArr.push(formData);
});
}
推荐答案
在推送到formArr之前,您可以简单地调用formData.setErrors({errorMessage:item.errorMessage}).
You can simply call formData.setErrors({errorMessage:item.errorMessage}) before pushing to formArr.
这篇关于我们如何在初始页面加载时动态设置表单数组控件上的错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!