【发布时间】:2019-07-28 08:25:48
【问题描述】:
我创建了一个由 JSON 数组组成的表单,据此,我正在生成 Validation、formControlName 并通过 formGroup 生成输出。
this.ELEMENT_DATA_UPDATE = [
{
first_name : 'abc',
last_name : 'xyz',
phone : 8888888888
}
];
在这里,我使用的是 Angular 材料,因此将此键值对转换为另一个由
组成的数组{"key" : "first_name" , "value" : "abc" , "name" : "First name :"}
这是输入 JSON 数组固定的时候。但是我的项目包含大规模的数据操作,其中输入的 JSON 数组内容会发生多次变化。生成 UI 模块没有问题,但是当我尝试将 Validation 和 forms 模块应用于这个动态生成的内容时,整个流程崩溃了,
这是我的代码
var jsonArray : any = [{}];
export class UpdateContactFieldDialog {
matcher = new MyErrorStateMatcher();
updateForm: FormGroup;
formString : string = null;
ELEMENT_DATA_UPDATE : any[] = [];
keyArray : any[] = [];
myJobFunctionControl = new FormControl();
optionsJobFunction: string[] = ['Operations', 'Production', 'Manufacturing'];
myTitleControl = new FormControl();
optionsTitle: string[] = ['HR', 'Developer', 'Tester', 'MD', 'CEO', 'Director'];
constructor(private formBuilder: FormBuilder,private dialogRef: MatDialogRef<AddContactFieldDialog> ) {
}
ngOnInit() {
//dumy randomly geneated fields
this.ELEMENT_DATA_UPDATE = [
{
first_name : 'abc',
last_name : 'xyz',
job_function : 'Production',
title : 'Developer',
email : 'abc@abx.com',
phone : 7945612304
}
];
for (let obj of this.ELEMENT_DATA_UPDATE) {
console.log("object length:", this.ELEMENT_DATA_UPDATE.length);
for (let key in obj) {
this.keyArray.push({'key' : key , 'value' : obj[key] , 'name': (key.charAt(0).toUpperCase() + key.substr(1).toLowerCase()).replace(/_/g, ' ')});
}
break;
}
console.log(this.keyArray);
console.log(this.ELEMENT_DATA_UPDATE[0]);
for(let formModule of this.keyArray){
var keyData = formModule.key;
if(this.formString != null && keyData == 'email' || keyData.includes('mail')){
this.formString = this.formString +''+keyData+':[this.ELEMENT_DATA_UPDATE[0].'+keyData +',[Validators.required,Validators.email]], ';
}
else
if(this.formString != null && keyData == 'phone' || keyData.includes('number') || keyData.includes('no') || keyData.includes('num') ){
this.formString = this.formString +''+keyData+':[this.ELEMENT_DATA_UPDATE[0].'+keyData +',[Validators.required,Validators.minLength(10),Validators.maxLength(10),Validators.pattern("[0-9]*")]], ';
}
else
if(this.formString == null && keyData != 'email' && keyData != 'phone'){
this.formString = ''+keyData+':[this.ELEMENT_DATA_UPDATE[0].'+keyData +',Validators.required], ';
}
else{
this.formString = this.formString + ''+keyData+':[this.ELEMENT_DATA_UPDATE[0].'+keyData +',Validators.required], ';
}
}
console.log(this.formString);
jsonArray = (this.formString);
this.updateForm = this.formBuilder.group(jsonArray);
}
// convenience getter for easy access to form fields
get f() { return this.updateForm.controls; }
submitContact() {
this.submitted = true;
// stop here if form is invalid
if (this.updateForm.valid) {
// alert('SUCCESS!! :-)\n\n' + JSON.stringify(this.updateForm.value))
console.log(this.updateForm.value);
this.dialogRef.close();
}
else{
return;
}
}
}
我的代码正在生成以下代码作为字符串
first_name: ['', Validators.required],
last_name: ['', Validators.required],
job_function: ['', [Validators.required]],
title: ['', [Validators.required]],
email: ['', [Validators.required, Validators.email]],
phone : ['',[Validators.required, Validators.minLength(10), Validators.maxLength(10), Validators.pattern('[0-9 ]*')]]
我希望在 formGroup 中使用它,这样我就可以动态生成 formControls ,为它们分配验证和值。
updateForm = this.formBuilder.group(jsonArray);
【问题讨论】:
-
不生成字符串,创建一个空的 FormGroup (myform=new FormGroup()) 并为每个 keyArray 使用 myForm.addcontrol (name,...) angular.io/api/forms/FormGroup#addcontrol
-
你能详细说明一下吗?因为我试过这样做。但你的概念可能不同。假设我有 5 个文档,我将从其中获取 5 个单独的密钥,并且需要对这 5 个密钥应用验证并动态使用 for。那么我应该如何用你的逻辑来做到这一点? @Eliseo