1414 - [Create a _reactive forms_ component](#create-component)
1515 - [Create its template file](#create-template)
1616 - [Import the _ReactiveFormsModule_](#import)
17- - [Update the app to use the form component](#update)
17+ - [Display the _HeroDetailComponent_](#update)
18+ - [Add a FormGroup](#formgroup)
19+ - [Taking a look at the form model](#json)
1820 - [Introduction to _FormBuilder_](#formbuilder)
21+ - [Validators.required](#validators)
1922 - [Nested FormGroups](#grouping)
2023 - [Inspect _FormControl_ properties](#properties)
2124 - [Set form model data using _setValue_ and _patchValue_](#set-data)
@@ -175,9 +178,6 @@ a#create-template
175178
176179+ makeExample('reactive-forms/ts/app/hero-detail-1.component.html' , 'simple-control' ,'app/hero-detail.component.html' )( format ="." )
177180
178-
179- //- +makeExample('reactive-forms/ts/app/hero-detail-2.component.html', 'basic-form','app/hero-detail.component.html')(format=".")
180-
181181.l-sub-section
182182 :marked
183183 Disregard the `form-control` _CSS_ class. It belongs to the
@@ -328,7 +328,7 @@ a#formbuilder
328328 clutter by handling details of control creation for you.
329329
330330 To use `FormBuilder`, you need to import it into `hero-detail.component.ts`:
331- + makeExample('reactive-forms/ts/app/hero-detail-3 .component.ts' , 'imports' ,'app/hero-detail.component.ts (excerpt)' )( format ="." )
331+ + makeExample('reactive-forms/ts/app/hero-detail-3a .component.ts' , 'imports' ,'app/hero-detail.component.ts (excerpt)' )( format ="." )
332332
333333:marked
334334 Use it now to refactor the `HeroDetailComponent` into something that's a little easier to read and write,
@@ -340,7 +340,7 @@ a#formbuilder
340340 * Call `createForm` in the constructor.
341341
342342 The revised `HeroDetailComponent` looks like this:
343- + makeExample('reactive-forms/ts/app/hero-detail-3 .component.ts' , 'v3 ' ,'app/hero-detail.component.ts (excerpt)' )( format ="." )
343+ + makeExample('reactive-forms/ts/app/hero-detail-3a .component.ts' , 'v3a ' ,'app/hero-detail.component.ts (excerpt)' )( format ="." )
344344
345345:marked
346346 `FormBuilder.group` is a factory method that creates a `FormGroup`.
@@ -350,6 +350,45 @@ a#formbuilder
350350 Defining a group of controls in a single object makes for a compact, readable style.
351351 It beats writing an equivalent series of `new FormControl(...)` statements.
352352
353+ a#validators
354+ :marked
355+ ### Validators.required
356+ Though this guide doesn't go deeply into validations, here is one example that
357+ demonstrates the simplicity of using `Validators.required` in reactive forms.
358+
359+ First, be sure to import it:
360+ + makeExample('reactive-forms/ts/app/hero-detail-3.component.ts' , 'imports' ,'app/hero-detail.component.ts (excerpt)' )( format ="." )
361+
362+ :marked
363+ Then, you can easily make the `name` `FormControl` required by supplying the `name`
364+ property in the class with an array that holds two arguments,
365+ the initial value for `name` and `Validators.required`.
366+
367+ + makeExample('reactive-forms/ts/app/hero-detail-3.component.ts' , 'required' ,'app/hero-detail.component.ts (excerpt)' )( format ="." )
368+
369+ :marked
370+ Now, by outputting the form's status in the template, you can see
371+ whether the form is `VALID` or `INVALID`. Add the following to the template:
372+
373+ + makeExample('reactive-forms/ts/app/hero-detail-3.component.html' , 'form-value-json' ,'app/hero-detail.component.html (excerpt)' )( format ="." )
374+
375+ :marked
376+ The browser displays the form's status according to `Validators.required`:
377+
378+ figure.image-display
379+ img( src ="/resources/images/devguide/reactive-forms/validators-json-output.png" width ="400px" alt ="Single FormControl" )
380+
381+ :marked
382+ `Validators.required` defaults
383+ to `INVALID` when the input has no value. Type into the input
384+ to watch the status change from `INVALID` to `VALID`.
385+
386+ Using `Validators.required` is optional for the rest of the guide.
387+ You may remove it as what follows does not use it.
388+
389+ For more on validating Angular forms, see the
390+ [Form Validation](../cookbook/form-validation.html) guide.
391+
353392:marked
354393 ### More FormControls
355394 A hero has more than a name. A hero has an address too. You already have address data
@@ -480,7 +519,7 @@ table(width="100%")
480519
481520 One common reason for inspecting `FormControl` properties is to
482521 make sure the user entered valid values.
483- Read about validating Angular forms in the
522+ Read more about validating Angular forms in the
484523 [Form Validation](../cookbook/form-validation.html) guide.
485524
486525.l-main-section
0 commit comments