Repository navigation
Expand file tree
/
Copy pathmain.ts
More file actions
152 lines (133 loc) · 4.11 KB
/
Copy pathmain.ts
File metadata and controls
152 lines (133 loc) · 4.11 KB
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
import { NgModule, Component, Pipe, OnInit } from "@angular/core";
import {
ReactiveFormsModule,
FormsModule,
FormGroup,
FormControl,
Validators,
FormBuilder
} from "@angular/forms";
import { BrowserModule } from "@angular/platform-browser";
import { platformBrowserDynamic } from "@angular/platform-browser-dynamic";
@Component({
selector: "model-form",
template: `<!--suppress ALL -->
<div class="container">
<form novalidate
[formGroup]="myform"
(ngSubmit)="onSubmit()">
<fieldset formGroupName="name">
<div class="form-group">
<label>First Name</label>
<input type="text"
class="form-control"
formControlName="firstName">
</div>
<div class="form-group">
<label>Last Name</label>
<input type="text"
class="form-control"
formControlName="lastName">
</div>
</fieldset>
<div class="form-group" [ngClass]="{
'has-danger': email.invalid && email.dirty,
'has-success': email.valid && email.dirty
}">
<label>Email</label>
<input type="email"
class="form-control"
formControlName="email">
<pre>Dirty? {{ myform.controls.email.dirty }}</pre>
<pre>Pristine? {{ myform.controls.email.pristine }}</pre>
<pre>Touched? {{ myform.controls.email.touched }}</pre>
<pre>Valid? {{ myform.controls.email.valid }}</pre>
<pre>Invalid? {{ myform.controls.email.invalid }}</pre>
</div>
<div class="form-group" [ngClass]="{
'has-danger': password.invalid && (password.dirty || password.touched),
'has-success': password.valid && (password.dirty || password.touched)
}">
<label>Password</label>
<input type="password"
class="form-control"
formControlName="password">
<div class="form-control-feedback"
*ngIf="password.errors && (password.dirty || password.touched)">
<p *ngIf="password.errors.required">Password is required</p>
<p *ngIf="password.errors.minlength">Password must be {{password.errors.minlength.requiredLength}} characters long, we need another {{password.errors.minlength.requiredLength - password.errors.minlength.actualLength}} characters </p>
</div>
<pre>{{ password.errors | json }}</pre>
</div>
<div class="form-group">
<label>Language</label>
<select class="form-control"
formControlName="language">
<option value="">Please select a language</option>
<option *ngFor="let lang of langs"
[value]="lang">{{lang}}
</option>
</select>
</div>
<pre>{{myform.value | json}}</pre>
<button type="submit" class="btn btn-primary" >Submit</button>
</form>
</div>
`
})
class ModelFormComponent implements OnInit {
langs: string[] = ["English", "French", "German"];
myform: FormGroup;
firstName: FormControl;
lastName: FormControl;
email: FormControl;
password: FormControl;
language: FormControl;
createFormControls() {
this.firstName = new FormControl("", Validators.required);
this.lastName = new FormControl("", Validators.required);
this.email = new FormControl("", [
Validators.required,
Validators.pattern("[^ @]*@[^ @]*")
]);
this.password = new FormControl("", [
Validators.required,
Validators.minLength(8)
]);
this.language = new FormControl("", Validators.required);
}
createForm() {
this.myform = new FormGroup({
name: new FormGroup({
firstName: this.firstName,
lastName: this.lastName
}),
email: this.email,
password: this.password,
language: this.language
});
}
ngOnInit() {
this.createFormControls();
this.createForm();
}
onSubmit() {
if (this.myform.valid) {
console.log("Form Submitted!");
console.log(this.myform.value);
this.myform.reset();
}
}
}
@Component({
selector: "app",
template: `<model-form></model-form>`
})
class AppComponent {}
@NgModule({
imports: [BrowserModule, FormsModule, ReactiveFormsModule],
declarations: [AppComponent, ModelFormComponent],
bootstrap: [AppComponent]
})
class AppModule {}
platformBrowserDynamic().bootstrapModule(AppModule);