这是我的表单组:
this.shopGroup = this.fb.group({
_user: [''],
name: ['', Validators.compose([Validators.required, Validators.maxLength(60)])],
url_name: [''],
desc: ['', Validators.compose([Validators.required, Validators.maxLength(600)])],
photos: [''],
currency: ['Real'],
language: ['Português do Brasil'],
address: this.fb.group({
zipcode: ['', Validators.compose([Validators.required, Validators.pattern('[0-9]{5}[\-]?[0-9]{3}')])],
street: ['', Validators.compose([Validators.required, Validators.maxLength(70)])],
number: [null, Validators.compose([Validators.required, Validators.max(99999)])],
complement: ['', Validators.maxLength(30)],
district: ['', Validators.compose([Validators.required, Validators.maxLength(60)])],
state: ['', Validators.required],
city: ['', Validators.compose([Validators.required, Validators.maxLength(70)])]
}),
status: [true],
created_at: [new Date()],
updated_at: [new Date()]
});
我需要将其转换为FormData,因为我要将图像上传到服务器(Multer包),但是,我不确定如何处理address
组,就像shopGroup
表单数据中的新对象一样。下面是我正在做的从FormGroup转换为FormData(地址不起作用):
const shopData: any = new FormData();
shopData.append('name', shopGroup.get('name').value);
shopData.append('zipcode', shopGroup.get('address').get('zipcode').value);
...
如何进行转换(Json到FormData)并处理像address
这样的嵌入/嵌套对象?
5条答案
按热度按时间bqjvbblv1#
好了,我找到了一个将JSON对象转换为FormData的函数:
zmeyuzjn2#
这应该是您的开始--尽管,您需要使其递归化以处理您提到的嵌套FormData。
avkwfej43#
也可以通过以下方式从html-form创建
FormData
对象:zrfyljdw4#
解决方案1.最简单的解决方案
但是解决方案1不包括
FormGroup nested in a FormArray
,例如解决方案2:覆盖
FormArray
以及嵌套在FormArray
中的FormGroup and FormControl
b4wnujal5#
FormGroupMap到FormData