/
index.ts
172 lines (149 loc) Β· 4.92 KB
/
index.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
/**
* @license
* Copyright Google LLC All Rights Reserved.
*
* Use of this source code is governed by an MIT-style license that can be
* found in the LICENSE file at https://angular.io/license
*/
/* tslint:disable:no-console */
import {Component, Host, NgModule} from '@angular/core';
import {AbstractControl, FormBuilder, FormGroup, FormGroupDirective, ReactiveFormsModule, UntypedFormBuilder, UntypedFormGroup, Validators} from '@angular/forms';
import {BrowserModule} from '@angular/platform-browser';
import {platformBrowserDynamic} from '@angular/platform-browser-dynamic';
/**
* Custom validator.
*/
function creditCardValidator(c: AbstractControl): {[key: string]: boolean} {
if (c.value && /^\d{16}$/.test(c.value)) {
return null;
} else {
return {'invalidCreditCard': true};
}
}
/**
* This is a component that displays an error message.
*
* For instance,
*
* <show-error control="creditCard" [errors]="['required', 'invalidCreditCard']"></show-error>
*
* Will display the "is required" error if the control is empty, and "invalid credit card" if the
* control is not empty
* but not valid.
*
* In a real application, this component would receive a service that would map an error code to an
* actual error message.
* To make it simple, we are using a simple map here.
*/
@Component({
selector: 'show-error',
inputs: ['controlPath: control', 'errorTypes: errors'],
template: `
<span *ngIf="errorMessage !== null">{{errorMessage}}</span>
`
})
export class ShowError {
formDir: FormGroupDirective;
controlPath: string;
errorTypes: string[];
constructor(@Host() formDir: FormGroupDirective) {
this.formDir = formDir;
}
get errorMessage(): string {
const form: FormGroup = this.formDir.form;
const control = form.get(this.controlPath);
if (control && control.touched) {
for (let i = 0; i < this.errorTypes.length; ++i) {
if (control.hasError(this.errorTypes[i])) {
return this._errorMessage(this.errorTypes[i]);
}
}
}
return null;
}
private _errorMessage(code: string): string {
const config: {[key: string]: string} = {
'required': 'is required',
'invalidCreditCard': 'is invalid credit card number',
};
return config[code];
}
}
@Component({
selector: 'reactive-forms',
viewProviders: [FormBuilder],
template: `
<h1>Checkout Form (Reactive)</h1>
<form (ngSubmit)="onSubmit()" [formGroup]="form" #f="ngForm">
<p>
<label for="firstName">First Name</label>
<input type="text" id="firstName" formControlName="firstName">
<show-error control="firstName" [errors]="['required']"></show-error>
</p>
<p>
<label for="middleName">Middle Name</label>
<input type="text" id="middleName" formControlName="middleName">
</p>
<p>
<label for="lastName">Last Name</label>
<input type="text" id="lastName" formControlName="lastName">
<show-error control="lastName" [errors]="['required']"></show-error>
</p>
<p>
<label for="country">Country</label>
<select id="country" formControlName="country">
<option *ngFor="let c of countries" [value]="c">{{c}}</option>
</select>
</p>
<p>
<label for="creditCard">Credit Card</label>
<input type="text" id="creditCard" formControlName="creditCard">
<show-error control="creditCard" [errors]="['required', 'invalidCreditCard']"></show-error>
</p>
<p>
<label for="amount">Amount</label>
<input type="number" id="amount" formControlName="amount">
<show-error control="amount" [errors]="['required']"></show-error>
</p>
<p>
<label for="email">Email</label>
<input type="email" id="email" formControlName="email">
<show-error control="email" [errors]="['required']"></show-error>
</p>
<p>
<label for="comments">Comments</label>
<textarea id="comments" formControlName="comments">
</textarea>
</p>
<button type="submit" [disabled]="!f.form.valid">Submit</button>
</form>
`
})
export class ReactiveForms {
form: UntypedFormGroup;
countries = ['US', 'Canada'];
constructor(fb: UntypedFormBuilder) {
this.form = fb.group({
'firstName': ['', Validators.required],
'middleName': [''],
'lastName': ['', Validators.required],
'country': ['Canada', Validators.required],
'creditCard': ['', Validators.compose([Validators.required, creditCardValidator])],
'amount': [0, Validators.required],
'email': ['', Validators.required],
'comments': ['']
});
}
onSubmit(): void {
console.log('Submitting:');
console.log(this.form.value);
}
}
@NgModule({
bootstrap: [ReactiveForms],
declarations: [ShowError, ReactiveForms],
imports: [BrowserModule, ReactiveFormsModule]
})
export class ExampleModule {
}
platformBrowserDynamic().bootstrapModule(ExampleModule);