DEV Community

Discussion on: Building scalable robusts and type safe forms with Angular

Collapse
 
purplenimbus profile image
Anthony Akpan

Unless I'm missing something here, how is this diffrent from the native angular form builder?

Dont quote me on this but I'm sorta sure that one can build complex forms in a child parent manner using just the form builder.

Generate the form in the root component and pass individual form groups, form arrays to the sub components

Collapse
 
maxime1992 profile image
Maxime • Edited

Hi Antony,

yes ngx-sub-forms is different than what you can do with FormBuilder.

FormBuilder is a simple helper to save you some boilerplate instead of using directly new FormGroup().

If you refer to breaking-down-the-form-into-sub-co... and breaking-down-the-form-into-sub-co..., I've explained why using a ControlValueAccessor instead of passing a FormGroup (or FormArray, FormControl, etc) is a better idea.

But ngx-sub-form is also going further than that:

  • Type safety (within .ts and .html)
  • Helpers (see what-raw-ngxsubform-endraw-has-to-...) including for example the retrieval of errors from a parent with nested ones too, that is not possible with form groups
  • Gives you methods, to have for a given form a different internal structure which is important for dealing with polymorphic data

It feels like I'm quoting myself from the article without adding much in this comment so not sure how helpful this will be to you... Let me know if you're still not convinced :). Maybe you can make a demo on stackblitz using inputs to pass a formGroup and I can fork your demo to show you the difference with ngx-sub-form 👍

Oh and I almost forgot, but using a ControlValueAccessor you let people decide whether they want to use a template form or a reactive one. So you could build a sub component with ngx-sub-form in a reactive way, and consume it as part of another form with a template syntax for example. Which is not the case when you pass a formGroup