/
hero-detail.component.ts
151 lines (134 loc) · 5.37 KB
/
hero-detail.component.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
import { Location } from '@angular/common';
import { Component, OnInit } from '@angular/core';
import { FormArray, FormGroup, ValidationErrors } from '@angular/forms';
import { ActivatedRoute, Params } from '@angular/router';
import { DemoWebApi_Controllers_Client, DemoWebApi_DemoData_Client } from '../../clientapi/WebApiCoreNG2FormGroupClientAuto';
export interface HeroWithNestedFormProperties extends DemoWebApi_Controllers_Client.HeroFormProperties {
address?: FormGroup<DemoWebApi_DemoData_Client.AddressFormProperties>,
phoneNumbers?: FormArray<FormGroup<DemoWebApi_DemoData_Client.PhoneNumberFormProperties>>,
}
export function CreateHeroWithNestedFormGroup() {
const fg: FormGroup<HeroWithNestedFormProperties> = DemoWebApi_Controllers_Client.CreateHeroFormGroup();
fg.controls.address = DemoWebApi_DemoData_Client.CreateAddressFormGroup();
fg.controls.phoneNumbers = new FormArray<FormGroup<DemoWebApi_DemoData_Client.PhoneNumberFormProperties>>([]);
return fg;
}
@Component({
selector: 'app-hero-detail',
templateUrl: './hero-detail.component.html'
})
export class HeroDetailComponent implements OnInit {
hero?: DemoWebApi_Controllers_Client.Hero;
heroForm: FormGroup<HeroWithNestedFormProperties>;
constructor(
private heroService: DemoWebApi_Controllers_Client.Heroes,
private route: ActivatedRoute,
private location: Location
) {
this.heroForm = CreateHeroWithNestedFormGroup();
}
ngOnInit(): void {
this.route.params.forEach((params: Params) => {
const id = +params['id'];
this.heroService.getHero(id.toString()).subscribe({
next: hero => {
if (hero) {
this.hero = hero;
this.heroForm.patchValue(hero); // populate properties including composit ones except nested array.
if (this.hero.phoneNumbers) {
this.hero.phoneNumbers.forEach(d => {
const g = DemoWebApi_DemoData_Client.CreatePhoneNumberFormGroup();
g.patchValue(d);
this.heroForm.controls.phoneNumbers?.push(g);
});
}
}
},
error: error => alert(error)
});
});
}
save(): void {
const raw: DemoWebApi_Controllers_Client.Hero = this.heroForm.getRawValue(); // nested array included.
this.heroService.put(raw).subscribe(
{
next: d => {
console.debug('response: ' + JSON.stringify(d));
},
error: error => alert(error)
}
);
}
goBack(): void {
this.location.back();
}
addPhoneNumber() {
var n = DemoWebApi_DemoData_Client.CreatePhoneNumberFormGroup();
this.heroForm.controls.phoneNumbers?.push(n);
}
removePhoneNumber(pg: FormGroup<DemoWebApi_DemoData_Client.PhoneNumberFormProperties>) {
const idx = this.heroForm.controls.phoneNumbers?.controls.indexOf(pg);
if (idx != undefined) {
this.heroForm.controls.phoneNumbers?.removeAt(idx);
}
}
getMinLengthErrorsText(errors: ValidationErrors | null) {
if (!errors) {
return '';
}
if (errors['minlength']) {
return 'Min length: ' + errors['minlength']['requiredLength'];
}
return '';
}
getMaxLengthErrorsText(errors: ValidationErrors | null) {
if (!errors) {
return '';
}
if (errors['maxlength']) {
return 'Max length: ' + errors['maxlength']['requiredLength'];
}
return '';
}
getErrorsText(errors: ValidationErrors | null | undefined) {
if (!errors) {
return '';
}
const lines: string[] = [];
for (const epn in errors) {
switch (epn) {
case 'minlength':
lines.push('Min length: ' + errors['minlength']['requiredLength']);
break;
case 'maxlength':
lines.push('Max length: ' + errors['maxlength']['requiredLength']);
break;
case 'email':
lines.push('Invalid Email address format'); //value is boolean false.
break;
case 'pattern':
lines.push('Invalid Web URL'); //value is requiredPattern and actualValue.
break;
default:
// print json text of the error during development
break;
}
}
return lines.join('; ');
}
allNestedValid(fg: FormGroup): boolean {
for (const cn in fg.controls) {
if (fg.controls[cn] instanceof FormGroup) { //also work well with FormArray
const r = this.allNestedValid(fg.controls[cn] as FormGroup);
if (!r) {
return false;
}
} else {
if (fg.controls[cn].invalid) {
return false;
}
}
}
return fg.valid;
}
}