Skip to content
This repository was archived by the owner on Dec 4, 2017. It is now read-only.

Commit 047c118

Browse files
docs(reactive-forms): Add Validators section, update screenshots and examples
1 parent a4f0c1b commit 047c118

File tree

6 files changed

+76
-8
lines changed

6 files changed

+76
-8
lines changed

public/docs/_examples/reactive-forms/ts/app/hero-detail-3.component.html

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,4 +11,5 @@ <h3><i>A FormGroup with a single FormControl using FormBuilder</i></h3>
1111

1212
<!-- #docregion form-value-json -->
1313
<p>Form value: {{ heroForm.value | json }}</p>
14+
<p>Form status: {{ heroForm.status | json }}</p>
1415
<!-- #enddocregion form-value-json -->

public/docs/_examples/reactive-forms/ts/app/hero-detail-3.component.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,9 @@ export class HeroDetailComponent3 {
1919

2020
createForm() {
2121
this.heroForm = this.fb.group({
22-
name: ['', Validators.required ], // <--- the FormControl called "name"
22+
// #docregion required
23+
name: ['', Validators.required ],
24+
// #enddocregion required
2325
});
2426
}
2527
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
/* tslint:disable:component-class-suffix */
2+
// #docregion imports
3+
import { Component } from '@angular/core';
4+
import { FormBuilder, FormGroup } from '@angular/forms';
5+
// #enddocregion imports
6+
7+
@Component({
8+
moduleId: module.id,
9+
selector: 'hero-detail-3',
10+
templateUrl: './hero-detail-3.component.html'
11+
})
12+
// #docregion v3a
13+
export class HeroDetailComponent3 {
14+
heroForm: FormGroup; // <--- heroForm is of type FormGroup
15+
16+
constructor(private fb: FormBuilder) { // <--- inject FormBuilder
17+
this.createForm();
18+
}
19+
20+
createForm() {
21+
this.heroForm = this.fb.group({
22+
name: '', // <--- the FormControl called "name"
23+
});
24+
}
25+
}
26+
// #enddocregion v3a

public/docs/ts/latest/guide/reactive-forms.jade

Lines changed: 46 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,11 @@ a#toc
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`. &nbsp;
@@ -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
31.8 KB
Loading
59.4 KB
Loading

0 commit comments

Comments
 (0)