Skip to content

Commit 0ae63fe

Browse files
committed
feat: update form validate
1 parent 35cc985 commit 0ae63fe

File tree

2 files changed

+44
-8
lines changed

2 files changed

+44
-8
lines changed

src/app/reactive/reactive.component.html

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,7 @@ <h1>Welcome</h1>
1919
formControlName="password"
2020
required
2121
/>
22-
<button
23-
class="button"
24-
type="submit"
25-
id="login-button"
26-
[disabled]="!loginForm.valid"
27-
>
22+
<button class="button" type="submit" id="login-button">
2823
Login
2924
</button>
3025
<br />
@@ -56,6 +51,19 @@ <h1>Welcome</h1>
5651
DELETE
5752
</button>
5853
</form>
54+
<p class="alert alert-danger" *ngIf="meta.submitted && emailInvalid">
55+
MAIL FORM: INVALID
56+
</p>
57+
<p
58+
class="alert alert-danger"
59+
*ngIf="meta.submitted && emailPatternInvalid"
60+
>
61+
MAIL FORM PATTERN: INVALID
62+
</p>
63+
<p class="alert alert-danger" *ngIf="meta.submitted && passwordInvalid">
64+
PASSWORD FORM: INVALID
65+
</p>
66+
<p>Form Status: {{ loginForm.status }}</p>
5967
</div>
6068

6169
<ul class="bg-bubbles">
@@ -70,5 +78,4 @@ <h1>Welcome</h1>
7078
<li></li>
7179
<li></li>
7280
</ul>
73-
<p>Form Status: {{ loginForm.status }}</p>
7481
</div>

src/app/reactive/reactive.component.ts

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,30 @@ import { Validators } from '@angular/forms';
1010
export class ReactiveComponent implements OnInit {
1111
constructor(private builder: FormBuilder) {}
1212

13+
get formStatus() {
14+
return this.loginForm.status === 'VALID' ? true : false;
15+
}
16+
get passwordInvalid() {
17+
return this.loginForm.controls.password.errors?.required
18+
? true
19+
: false ?? false;
20+
}
21+
get emailInvalid() {
22+
return this.loginForm.controls.mail.errors?.required
23+
? true
24+
: false ?? false;
25+
}
26+
get emailPatternInvalid() {
27+
return this.loginForm.controls.mail.errors?.email
28+
? true
29+
: false ?? false;
30+
}
31+
32+
// controls form
33+
protected meta = {
34+
submitted: false as boolean,
35+
};
36+
1337
// Using FormBuilder to link and initialize form values
1438
public loginForm = this.builder.group({
1539
mail: ['', Validators.email], // validates whether email format or not
@@ -28,7 +52,12 @@ export class ReactiveComponent implements OnInit {
2852
* Submit form values
2953
*/
3054
submit() {
31-
window.alert(JSON.stringify(this.loginForm.value));
55+
this.meta.submitted = true;
56+
console.log(this.loginForm);
57+
console.log(this.loginForm.status);
58+
if (this.formStatus) {
59+
window.alert(JSON.stringify(this.loginForm.value));
60+
}
3261
}
3362

3463
/**

0 commit comments

Comments
 (0)