Lessons: 67Length: 8.9 hours

Next lesson playing in 5 seconds

Cancel
  • Overview
  • Transcript

5.5 Using a `FormBuilder`

In this lesson, we'll look at the FormBuilder class. This utility is provided by Angular to make creating reactive forms less repetitive.

1.Introduction
6 lessons, 42:00

1.1
Introduction
00:48

1.2
Get Started With Angular-CLI
11:09

1.3
Developing With Angular-CLI
13:17

1.4
TypeScript vs. JavaScript
06:54

1.5
Angular Modules From the CLI
04:31

1.6
CLI Options
05:21

2.Get Started With Angular
7 lessons, 42:38

2.1
Bootstrapping the Application
04:30

2.2
The Application Module
04:15

2.3
The Application Component
08:06

2.4
Component Styling
03:06

2.5
Global Styling
05:11

2.6
Creating a Component With the CLI
09:34

2.7
Creating a Service With the CLI
07:56

3.Core Concepts
7 lessons, 55:20

3.1
Component Trees
06:20

3.2
Dependency Injection
06:52

3.3
Content Projection
05:38

3.4
Component and Directive Lifecycle Methods
06:31

3.5
Component-Only Lifecycle Methods
05:28

3.6
Decorators
07:36

3.7
Models
16:55

4.Template Deep Dive
11 lessons, 1:10:56

4.1
Basic Data Binding With Interpolation
05:35

4.2
Property Bindings
07:07

4.3
Attribute Bindings
03:29

4.4
Event Bindings
08:16

4.5
Class and Style Bindings
05:44

4.6
The `NgClass` and `NgStyle` Directives
05:04

4.7
The `*ngIf` Directive
04:41

4.8
The `*ngFor` Directive
09:29

4.9
Inputs
05:33

4.10
Using Pipes in a Template
07:31

4.11
Using Pipes in a Class
08:27

5.Forms
10 lessons, 1:45:41

5.1
Handling User Input With Template Reference Variables
07:06

5.2
Template-Driven Forms
11:10

5.3
Template-Driven Forms: Validation and Submission
14:00

5.4
Reactive Forms
11:26

5.5
Using a `FormBuilder`
08:01

5.6
Reactive Validation With Built-in Validators
14:53

5.7
Creating Custom Validators for Template-Driven Forms
12:18

5.8
Creating Custom Validators for Reactive Forms
08:26

5.9
Observing Form State Changes
12:40

5.10
Working With the `@HostListener` Decorator
05:41

6.Routing
9 lessons, 1:15:10

6.1
Defining and Configuring Routes
07:53

6.2
Rendering Components With Router Outlets
10:14

6.3
Using Router Links for Navigation
05:25

6.4
Navigating Routes Using the Router
06:24

6.5
Determining the Active Route Using an Activated Route
07:16

6.6
Working With Route Parameters
10:42

6.7
Using Route Guards
07:36

6.8
Observing Router Events
10:55

6.9
Adding Child Routes
08:45

7.Using the HTTP Client
5 lessons, 56:24

7.1
Sending an HTTP Request
10:52

7.2
Handling an HTTP Response
11:22

7.3
Setting Request Headers
12:33

7.4
Intercepting Requests
09:04

7.5
Finishing the Example Application
12:33

8.Testing
10 lessons, 1:23:27

8.1
Service Unit Test Preparation
10:45

8.2
Unit Testing Services
13:24

8.3
Component Unit Test Preparation
12:35

8.4
Unit Testing Components
07:27

8.5
Unit Testing Component Templates
06:58

8.6
Unit Testing Pipes
04:41

8.7
Unit Testing Directives
04:56

8.8
Unit Testing Validators
04:48

8.9
Unit Testing Observables
11:37

8.10
Unit Testing HTTP Interceptors
06:16

9.Building for Production
1 lesson, 03:40

9.1
Building for Production
03:40

10.Conclusion
1 lesson, 01:32

10.1
Conclusion
01:32


5.5 Using a `FormBuilder`

Hello folks, in this lesson we're gonna see how we can use a special FormBuilder class to make creating reactive forms less repetitive and manual. FormBuilder isn't required and for our simple feedback form, is not even necessary. But as forms get bigger and have more complex requirements, using a FormBuilder is definitely recommended. So first of all then, we'll need to bring the FormBuilder class into the component. The FormBuilder class also comes from the forms module. So now we'll need to inject the FormBuilder into the constructor. We won't need to use the FormBuilder in the template, so we can mark it as private, and Angular will manage the instance and the property of the class for us, great. So now, instead of creating a form group and individual form controls manually, we can use the FormBuilder. The FormBuilder has a method called group, which we can get from the instance that we injected into the constructor. This method also takes an object literal, and the keys in the object will still refer to form controls. We just don't need to create these form control instances manually. Instead, we can pass empty strings. And these refer to the initial values for the form controls. So, all of the controls will be empty to start with, which is what we want. Let's go back to the browser at this point. And we'll open up the feedback form, and we can see that everything looks exactly the same as it did before. It still looks hideous, but it's working in the same way that it did before. So now, we'll also want to add a button that can be clicked to actually send the feedback, and a way to cancel out and close the form again. So let's add these to the bottom of the template. We can use the disabled binding on the send button and we'll keep it disabled until the form is valid. And notice that we can bind directly from the feedback form from the template here, even though we don't have a template reference variable. And that's useful. We've also added this hide feedback form method to the span to close the form. The problem we have is that the logic to show the form is in the home component. But the button to close the form is in the feedback component. But it's not really a problem. You might guess how we can do it, as we looked at it briefly earlier. We can use a custom event to signal from the form to the home component that it should close the form. We'll need to add the Output and EventEmitter objects to our imports, first of all. And then we need to define the output event using the output decorator. Then we can add the hide feedback form event handler and have it emit our new custom event. Great, so that's nice and simple. So now we can bind to this closedFeedbackForm event in the home components template. So, the event is closedFeedbackForm, which in the left hand side of the expression. And the method that we'll use to handle that event is called hideFeedbackForm. So, we can add this method in the home components class now. We just set the property controlling whether the form is included in the DOM to false. So at this point now, we should be able to both show and hide the feedback form. So the form still looks terrible at this point. Why don't we have some styling for that quickly, just so that we can tidy that up. So we've added a new control, first of all, the text area and that hasn't used anywhere else. But we want this to have pretty much the same styling as a regular input, so in form CSS, let's just add a selector for text areas alongside the selector for inputs. And let's add some positioning and layout styling for the feedback form itself in app.sess. So let's just add it to the bottom. So it's pretty basic stuff that just positions the form under the feedback link as if it were a dropdown menu. We're keeping the styling light to match the site, and I don't want to get into it too deeply as this isn't a CSS tutorial. So, let's just go back and see how the form looks now. So, not too bad. It looks like there's something wrong with the padding on the outer container div. Nice, okay, so it's not the best looking form in the world, but it's tidy and it matches the rest of the site. So, I'm happy enough with that. So in this lesson we saw how to use Angular's FormBuilder to make creating a reactive form easier and less repetitive. We saw that we use the group method of the FormBuilder to create the form group that represents the form. And we passed an object to this, which specifies the controls that we want to create. We used empty strings with our controls so that the controls don't have any initial value. And we saw that we don't have to create instances now of either the form group or form control objects, which is really the main purpose of the FormBuilder. Thanks for watching.

Back to the top