This tutorial is out of date and no longer maintained.
With the Angular 2’s new forms module, we can build complex forms with even more intuitive syntax.
In this article, we will learn about how to build a nested model-driven form with validation using the latest forms module. If you are new to model-driven forms, please refer to How to Build Model-driven Forms in Angular 2](https://www.digitalocean.com/community/tutorials/using-angular-2s-model-driven-forms-with-formgroup-and-formcontrol) for a basic rundown.
Let’s say you have a product with a name and a price in your model. You want to create a form for it that allows a user to add a new product. But what if you want to add multiple products in the same form? How would we loop through each user model and validate it?
Nested forms allow us to handle multiple models in a single form.
Let’s dig a little deeper and see how we do it.
Here’s what we’ll be building:
We’re going to split our app up into multiple implementations:
Part 1: Create the customer form
View Angular 2 - Nested Model Driven Form (final) scotch - Part 1 on plnkr
Part 2: Move the group of controls to a new component
Angular 2 - Nested Model Driven Form (final) scotch - Part 2 on plnkr
We will build a form to capture customer information based on this interface:
Our app will be able to do the following:
Our end result for the Add Customer form will look like this:
Let’s take a look at how our app will be setup. We’ll have our app
folder which contains our components as well as our index.html
file, stylesheet, and tsconfig.json
.
|- app/
|- address.component.html
|- address.component.ts
|- app.component.html
|- app.component.ts
|- app.module.ts
|- main.ts
|- customer.interface.ts
|- index.html
|- styles.css
|- tsconfig.json
In order to use new forms module, we need to npm install @angular/forms
npm package and import the reactive forms module in application module.
Here’s the module for our application app.module.ts
:
Let’s move on to create our app component. app.component.ts
is our root component and we will write our component code here.
This is what our HTML view will look like:
We bind myForm
to form group directive. The Submit button will be disabled if the form is not valid. save
function will be called when we submit the form.
Now, let’s initialize our form model and create the functions that allow us to add and remove an address.
We’re using the form builder, _fb
, to create our form.
There are 3 functions available in the form builder:
Each form control accepts an array. The first parameter is the default value of the control, the second parameter accepts either a validator or an array of validators, and the third parameter is the async validator. Please refer to Angular official documentation for details.
In our example, we’ll assign a list of validators to the name
control, and assign a single validator to the street
control.
Great! All the necessary functions are created. Let’s now bind our form model to the view.
A few notes here:
formControlName
directive: the form control name.formArrayName
directive: the array name. In our example, we bind addresses to the formArrayName.formGroupName
directive: the form group name. Since addresses is an array, Angular assigns the index number as the group name to each of the addresses. Therefore, we’ll bind the index i
, to formGroupName
.Our form is working fine now. But imagine that you have a huge form which consists of a lot of controls, we might need to consider moving each group of controls to a separate component to keep our code neat.
Let’s move our address implementation to a new component.
Now, copy the address implementation from the app component view to the address view.
Let’s import the address component to our app module.
We can now replace address with our new address component in app component.
Voilà! With the new forms module, we can use formArray
to create a list of controls. We can separate each group of controls to a new component and the validation is still working fine.
That’s it. Happy coding!
Thanks for learning with the DigitalOcean Community. Check out our offerings for compute, storage, networking, and managed databases.
This textbox defaults to using Markdown to format your answer.
You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!