I have an issue with reactive forms in angular 2. I want to create my object to send it to my API but my ReactiveForms generate also no required field and formgroup.
If I send directly my object thanks to http post request I have this JSON :
MyObject{
Name:"toto"
Surname:""
Address:{
City:"",
StreetName:""
}
}
But I want this JSON to agree with the API. (If there is empty or null property/object I obtain an error because the fields are present but not valid:
MyObject{
Name:"toto"
}
Because Surname and Address is optionnal.
I use ReactiveForm with formBuilder and I do not know how I can obtain only fill fields.
This how I create my form :
myForm: FormGroup;
myUserModel : UserModel; // (Interface UserModel such as my form fields)
constructor(private fb : FormBuilder){}
createForm(){
this.myFrom = this.fb.FormGroup({
Name:['';Validators.requires],
Surname:'',
Address:this.fb.FormFroup({
City: '',
StreetName:''
})
})
}
onSubmit(){
this.myUserModel = this.myForm.value;
this.registerUserToAPI(this.myUserModel);
}
To cheat I use this but it's so dirty... :
cleanObjectToSend(obj){
return this.removeEmptyObj(this.removeEmptyAttribute(obj));
}
removeEmptyAttribute = (obj) => {
Object.keys(obj).forEach(key => {
if (obj[key] && typeof obj[key] === 'object') this.removeEmptyAttribute(obj[key]);
else if (obj[key] == null || obj[key] == '') delete obj[key];
});
return obj;
};
removeEmptyObj(obj){
Object.keys(obj).forEach(key => {
if (obj[key] && typeof obj[key] === 'object') {
if(Object.keys(obj[key]).length===0 && obj[key].constructor == Object){
delete obj[key];
return;
}
else this.removeEmptyObj(obj[key]);
}
else if (obj[key] == null || obj[key] == '') delete obj[key];
});
return obj;
}
It allow me to obtain only not null and not empty field to generate my JSON then.
How can I have a JSON clean JSON directly without null or empty value ?
Ok with your answer Lazar Ljubenović but there is no other way to make this in angular? Use an other form type such as Template Driven Form and ngModel for example ?
Update :
I think this is the better way to clean your JSON Object without delete. It works with nested object !