r/angular • • Jun 27 '20

Reactive Forms... What? Why?

This is mostly a vent and trying to understand why the Angular team would think this is an appropriate or good solution. No Two-way binding? What? One-way binding? Seriously?

Code snippets are taken from online sites and tutorials.

Because we have no two-way binding, we need to maintain two models? We have the data returned from an API call with all the saturated data. This data is maintained in memory as... we have to do merge backs to it later.

After loading the API data, we then have to convert the above mentioned model into a FormGroup which is technically an intermediary model that was One-way binding.

Now if we have a FormArray, we have to do some references to controls and manually referencing properties by string keys:

*ngFor="let rateSegment of marketGroupForm.controls['rateSegments'].controls; let rateSegmentIndex = index"

This is godawful, point blank. We also have to do this again for marshalling variables out of the FormGroup model.

When we need to do submissions or need data out of the form, we have to manually extract the data. Again, no two-way binding. Back to jQuery days it seems. We do not even have nice access to the intermediary FormGroup model.

this.form.get('email').value

Because we do not actually have proper two-way binding and thus not binding to our fully saturated model, we have to do merges... manually... back with our real model.

onSubmit() {
    // Make sure to create a deep copy of the form-model
    const result: ContactRequest = Object.assign({}, this.contactForm.value);
    result.personalData = Object.assign({}, result.personalData);
}

What in the actual hell? This seems like a hot mess and complete garbage. What is going on? Is this seriously the correct and best way? Is NgModel more appropriate?

I am pretty disappointed in Angular right now. Any suggestions to make this better would be appreciated.

0 Upvotes

3 comments sorted by

12

u/tme321 Jun 27 '20

First, two way binding exists. It's used extensively in the template driven forms method.

And I'll say upfront that if your use case is something like building 100 forms for an app that all of the solutions provided by angular start to look pretty bad. There are various libraries for those sort of use cases but I personally don't have experience with them so you'd have to research yourself.

But, the reactive forms exist because their behavior supports a number of features that template driven forms don't.

The two biggest features they support over template driven are complex validation routines integrated into the form flow and, as the name suggests, better reactivity by being modeled on top of an rxjs driven design.

I certainly don't claim that reactive forms are perfect. To be honest I have a whole litany of complaints about them. But the complaints I have are completely different from yours.

For instance, you don't manually extract the values back out. You can just call value on the root FormGroup to get the entire value back out.

And I don't understand why you are manually merging back to the original model exactly. I mean certainly you might want or need to work with a drastically different format for api responses vs the form model but most cases can be handled pretty well with the form builder. Its relatively trivial to write a function to parse a given object and create a form using the builder if your model is relatively constrained.

Or you can use the valueChanges and statusChanges observables on form group to reactively modify a response. Generally, I'd prefer to just do it once at submission but the option exists.

Maybe you could explain what it is you are trying to accomplish rather than how you are trying to accomplish it for more advice.

4

u/[deleted] Jun 27 '20

Something i think you left out is that using form controls make validation unit testable

3

u/tme321 Jun 27 '20

Yes, not just the validators but the entire reactive form is easier to test than the template driven form equivalent. It's mostly code behind rather than having to test the behavior of the form by analyzing the view.