Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Nested FormArrays not built correctly anymore #192

Closed
jbjhjm opened this issue Feb 3, 2022 · 4 comments · Fixed by #194
Closed

Nested FormArrays not built correctly anymore #192

jbjhjm opened this issue Feb 3, 2022 · 4 comments · Fixed by #194
Assignees
Labels
bug Something isn't working done

Comments

@jbjhjm
Copy link

jbjhjm commented Feb 3, 2022

While diving deeper into updating my app to DFB v2, it seems like one more feature does not work as expected anymore.
Still having a hard time to understand what does what exactly, but I think I hunted it down to where the logic is missing.

Assuming a model like this:

export class ExamQuestionAnswer {
	@MinLength(3, {always:true})
	@Expose()
	text:string='';
	@Expose()
	isCorrect:boolean=false;
}

export class ExamQuestionItem {
	@MinLength(3, {always:true})
	@Expose()
	question:string='';
	@Expose()
	@Type(() => ExamQuestionAnswer)
	@ValidateNested({always:true})
	answers:ExamQuestionAnswer[] = [];
}

export class Exam {
	@Expose()
	@Type(() => ExamQuestionItem)
	@ValidateNested({always:true})
	questions?:ExamQuestionItem[] = [];
}

Creating rootFormGroup will successfully create first-level array at Exam.questions.
Then, when patching data using setObject, Exam.questions will be populated with ExamQuestionItems.
However ExamQuestionItems.answers will not be generated correctly.
DFB will generate a FormControl instead of a FormArray and ExamQuestionItems.answers value will become the first array entry.

It looks like setValuesForControls implementation misses the logic to create nested FormArrays.
Instead it falls back to using the standard formBuilder which is not able to handle the model (ExamQuestionItem) correctly.

arrayControl.push(formBuilder.group(item))
@EndyKaufman EndyKaufman added the bug Something isn't working label Feb 4, 2022
@EndyKaufman EndyKaufman self-assigned this Feb 4, 2022
@EndyKaufman EndyKaufman linked a pull request Feb 9, 2022 that will close this issue
@EndyKaufman
Copy link
Owner

@jbjhjm I seem to have fixed this bug, please update to the latest version, if you add items try to use childFormGroup

@EndyKaufman EndyKaufman added done bug Something isn't working and removed bug Something isn't working labels Feb 9, 2022
@jbjhjm
Copy link
Author

jbjhjm commented Feb 9, 2022

oh awesome! Was working on the same today but this looks better!
I'll need to rebase tomorrow as I'm still on a custom branch for handling recursion.
Will let you know then! Did you receive my mail sent to admin@site15.ru btw? Asked about donations and freelance work!

@EndyKaufman
Copy link
Owner

no, I don’t see it, I just saw it and answered, I’m busy with work, I missed the letter, sorry

@jbjhjm
Copy link
Author

jbjhjm commented Feb 10, 2022

Alright seems to work nicely! Thanks for the fast fix!

@jbjhjm jbjhjm closed this as completed Feb 10, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working done
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants