I have a reactive form that when submitted, needs to round some of the values submitted, without effecting how the values look on the page.
To do this, I created a method that creates a new form and rounds the values, then returns for the actual form submission:
mapFormValues(): FormGroup {
const newForm: FormGroup = this.myOriginalForm;
const fieldsToRound = ['field1', 'field2', 'field3'];
fieldsToRound.forEach((field: string) => {
if (newForm.get(field).value >= 1000) {
const formField = newForm.get(field).value / 1000;
newForm.get(field).setValue(formField);
}
});
return newForm;
}
The problem is that, since my newForm
is just a reference to my original form, it still changes the original form values on the page itself.
How can I create a copy of the form that I can transform values on, without changing my original form?
I looked at Clone Object without reference javascript but it seems I can't do this with FormGroups.