3

I have a formGroup called 'selsectedForm' includes nested arrays, the first level is 'fields' formArray includes the second level which is 'values' formArray.

I saw many examples about nested form arrays and tried to use them but I failed every time.

I couldn't use setValue() or patchValue() methods in these nested arrays to update the formGroup values.

I need to do that to see if the form is touched and valid, and also to update the values for submit.

here is the code: https://stackblitz.com/edit/angular-httakx?embed=1&file=app/app.component.ts

1 Answer 1

1

You can set a debugger in your app and print the form so you can check the fields of the value attribute to set the exact content it needs.

For the addField() function:

last.patchValue({values:['myValue']})

For the addValue(i) function:

var storedValue = values.getRawValue(); storedValue[storedValue.length - 1] = 'value' + i; values.patchValue(storedValue)

For removeValue(i,j):

var storedValue = values.getRawValue(); storedValue[storedValue.length - 1] = ''; values.patchValue(storedValue)

And you don't need anything to remove value when removing the field, just removing the form control.

Hope it helps

Sign up to request clarification or add additional context in comments.

2 Comments

I modified the code and added 'print the form in the console' button, try to add new field an fill the values, then click this button, you will see in the console that the new field is not in the form values
another note, if you add new fields without filling the first one, then the form still be not touched.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.