Skip to content

Commit 3198ab2

Browse files
committed
feat: introduce validate into template-driven
1 parent da1df26 commit 3198ab2

File tree

2 files changed

+66
-7
lines changed

2 files changed

+66
-7
lines changed

src/app/template-driven/template-driven.component.html

Lines changed: 44 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,27 +7,65 @@ <h1>Welcome</h1>
77
<!-- ① using ngForm to link form with component -->
88
<form class="form" #form="ngForm" (ngSubmit)="submit()">
99
<!-- ② using ngModel to link value with component model -->
10-
<input type="email" placeholder="mail" [(ngModel)]="model.mail" name="mail" #mail="ngModel" />
11-
<input type="password" placeholder="password" [(ngModel)]="model.password" name="password" #password="ngModel" />
12-
<button class="button" type="submit" id="login-button">Login</button>
10+
<input
11+
type="email"
12+
placeholder="mail"
13+
[(ngModel)]="model.mail"
14+
name="mail"
15+
#mail="ngModel"
16+
required
17+
email
18+
/>
19+
<input
20+
type="password"
21+
placeholder="password"
22+
[(ngModel)]="model.password"
23+
name="password"
24+
#password="ngModel"
25+
required
26+
/>
27+
<button class="button" type="submit" id="login-button">
28+
Login
29+
</button>
1330
<br />
14-
<button class="button fill-button mail" type="button" id="fill-mail-button" [disabled]="!form.valid" (click)="fillMail()">
31+
<button
32+
class="button fill-button mail"
33+
type="button"
34+
id="fill-mail-button"
35+
(click)="fillMail()"
36+
>
1537
Fill Mail
1638
</button>
1739
<button
1840
class="button fill-button password"
1941
type="button"
2042
id="fill-password-button"
21-
[disabled]="!form.valid"
2243
(click)="fillPassword()"
2344
>
2445
Fill Password
2546
</button>
2647
<br />
2748
<br />
2849
<br />
29-
<button class="button delete" type="button" id="delete-button" (click)="deleteValue()">DELETE</button>
50+
<button
51+
class="button delete"
52+
type="button"
53+
id="delete-button"
54+
(click)="deleteValue()"
55+
>
56+
DELETE
57+
</button>
3058
</form>
59+
<p class="alert alert-danger" *ngIf="emailInvalid">
60+
MAIL FORM: INVALID
61+
</p>
62+
<p class="alert alert-danger" *ngIf="emailPatternInvalid">
63+
MAIL FORM PATTERN: INVALID
64+
</p>
65+
<p class="alert alert-danger" *ngIf="passwordInvalid">
66+
PASSWORD FORM: INVALID
67+
</p>
68+
<p>Form Status: {{ form.status }}</p>
3169
</div>
3270

3371
<ul class="bg-bubbles">

src/app/template-driven/template-driven.component.ts

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,33 @@ import { FormBuilder, NgForm } from '@angular/forms';
88
})
99
export class TemplateDrivenComponent implements OnInit {
1010
constructor() {}
11+
12+
get formStatus() {
13+
return this.form?.status === 'VALID' ? true : false;
14+
}
15+
get passwordInvalid() {
16+
return this.form?.form?.controls?.password?.errors?.required
17+
? true
18+
: false ?? false;
19+
}
20+
get emailInvalid() {
21+
return this.form?.form?.controls?.mail?.errors?.required
22+
? true
23+
: false ?? false;
24+
}
25+
get emailPatternInvalid() {
26+
return this.form?.form?.controls?.mail?.errors?.email
27+
? true
28+
: false ?? false;
29+
}
30+
1131
public model = {
1232
mail: '',
1333
password: '',
1434
};
1535

36+
// https://angular.io/guide/form-validation
37+
1638
@ViewChild('form')
1739
form: NgForm;
1840

@@ -22,7 +44,6 @@ export class TemplateDrivenComponent implements OnInit {
2244
* Submit form values
2345
*/
2446
submit() {
25-
console.log('submit');
2647
window.alert(JSON.stringify(this.form.value));
2748
}
2849

0 commit comments

Comments
 (0)