Skip to content

Commit 35cc985

Browse files
committed
feat: introduce form validate
1 parent 0cb3d17 commit 35cc985

File tree

2 files changed

+39
-10
lines changed

2 files changed

+39
-10
lines changed

src/app/reactive/reactive.component.html

Lines changed: 35 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,26 +7,54 @@ <h1>Welcome</h1>
77
<!-- ① using formGroup to link form with component -->
88
<form class="form" [formGroup]="loginForm" (ngSubmit)="submit()">
99
<!-- ② using formControlName to link form with component's attribute -->
10-
<input type="email" placeholder="mail" formControlName="mail" />
11-
<input type="password" placeholder="Password" formControlName="password" />
12-
<button class="button" type="submit" id="login-button" [disabled]="!loginForm.valid">Login</button>
10+
<input
11+
type="email"
12+
placeholder="mail"
13+
formControlName="mail"
14+
required
15+
/>
16+
<input
17+
type="password"
18+
placeholder="Password"
19+
formControlName="password"
20+
required
21+
/>
22+
<button
23+
class="button"
24+
type="submit"
25+
id="login-button"
26+
[disabled]="!loginForm.valid"
27+
>
28+
Login
29+
</button>
1330
<br />
14-
<button class="button fill-button mail" type="button" id="fill-mail-button" [disabled]="!loginForm.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]="!loginForm.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>
3159
</div>
3260

@@ -42,4 +70,5 @@ <h1>Welcome</h1>
4270
<li></li>
4371
<li></li>
4472
</ul>
73+
<p>Form Status: {{ loginForm.status }}</p>
4574
</div>

src/app/reactive/reactive.component.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { Component, OnInit } from '@angular/core';
22
import { FormBuilder, FormControl, FormGroup } from '@angular/forms';
3+
import { Validators } from '@angular/forms';
34

45
@Component({
56
selector: 'app-reactive',
@@ -11,8 +12,8 @@ export class ReactiveComponent implements OnInit {
1112

1213
// Using FormBuilder to link and initialize form values
1314
public loginForm = this.builder.group({
14-
mail: [''],
15-
password: [''],
15+
mail: ['', Validators.email], // validates whether email format or not
16+
password: ['', Validators.required], // validates form filled or not
1617
});
1718

1819
/**
@@ -27,7 +28,6 @@ export class ReactiveComponent implements OnInit {
2728
* Submit form values
2829
*/
2930
submit() {
30-
console.log('submit');
3131
window.alert(JSON.stringify(this.loginForm.value));
3232
}
3333

@@ -61,7 +61,7 @@ export class ReactiveComponent implements OnInit {
6161
* Delete form values
6262
*/
6363
deleteValue() {
64-
this.loginForm.patchValue({
64+
this.loginForm.setValue({
6565
mail: '',
6666
password: '',
6767
});

0 commit comments

Comments
 (0)