Skip to content

Commit fae81e6

Browse files
authored
Merge pull request #5 from actionanand/features/2-reactive-form
Features/2 reactive form
2 parents 7e108e2 + 7183b77 commit fae81e6

File tree

3 files changed

+89
-24
lines changed

3 files changed

+89
-24
lines changed

src/app/reactive/login/login.component.ts

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ const reqSymbol = '?';
88
const reservedEmail = 'ar@example.com';
99

1010
function isPasswordContainReqChar(control: AbstractControl) {
11-
if (control.value.includes(reqSymbol)) {
11+
if (control.value?.includes(reqSymbol)) {
1212
return null;
1313
} else {
1414
return { PassNotHaveSymbol: true };
@@ -86,6 +86,14 @@ export class LoginComponent2 implements OnInit {
8686
}
8787

8888
onSubmit() {
89+
if (this.formObj.invalid) {
90+
return;
91+
}
92+
8993
// this.formObj.controls.emailField.addValidators
94+
console.log('this.formObj.controls.emailField => ', this.formObj.controls.emailField);
95+
console.log('this.formObj.value => ', this.formObj.value);
96+
97+
this.formObj.reset();
9098
}
9199
}
Lines changed: 24 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,23 @@
1-
<form>
1+
<form [formGroup]="formObj" (ngSubmit)="onSubmit()">
22
<h2>Welcome on board!</h2>
33
<p>We just need a little bit of data from you to get you started 🚀</p>
44

55
<div class="control">
66
<label for="email">Email</label>
7-
<input id="email" type="email" name="email" />
7+
<!-- [formControl]="formObj.controls.email" we can register 'control' like this also -->
8+
<input id="email" type="email" name="email" formControlName="email" />
89
</div>
910

10-
<div class="control-row">
11+
<div class="control-row" formGroupName="passwords">
1112
<div class="control">
1213
<label for="password">Password</label>
13-
<input id="password" type="password" name="password" />
14+
<!-- [formControl]="formObj.controls.passwords.controls.password" we can register 'control' like this also -->
15+
<input id="password" type="password" name="password" formControlName="password" />
1416
</div>
1517

1618
<div class="control">
1719
<label for="confirm-password">Confirm Password</label>
18-
<input id="confirm-password" type="password" name="confirm-password" />
20+
<input id="confirm-password" type="password" name="confirm-password" formControlName="confirmPass" />
1921
</div>
2022
</div>
2123

@@ -25,39 +27,39 @@ <h2>Welcome on board!</h2>
2527
<div class="control-row">
2628
<div class="control">
2729
<label for="first-name">First Name</label>
28-
<input type="text" id="first-name" name="first-name" />
30+
<input type="text" id="first-name" name="first-name" formControlName="firstName" />
2931
</div>
3032

3133
<div class="control">
3234
<label for="last-name">Last Name</label>
33-
<input type="text" id="last-name" name="last-name" />
35+
<input type="text" id="last-name" name="last-name" formControlName="lastName" />
3436
</div>
3537
</div>
3638

37-
<fieldset>
39+
<fieldset formGroupName="address">
3840
<legend>Your Address</legend>
3941

4042
<div class="control-row">
4143
<div class="control">
4244
<label for="street">Street</label>
43-
<input type="text" id="street" name="street" />
45+
<input type="text" id="street" name="street" formControlName="street" />
4446
</div>
4547

4648
<div class="control">
4749
<label for="number">Number</label>
48-
<input type="text" id="number" name="number" />
50+
<input type="text" id="number" name="number" formControlName="number" />
4951
</div>
5052
</div>
5153

5254
<div class="control-row">
5355
<div class="control">
5456
<label for="postal-code">Postal Code</label>
55-
<input type="text" id="postal-code" name="postal-code" />
57+
<input type="text" id="postal-code" name="postal-code" formControlName="postal" />
5658
</div>
5759

5860
<div class="control">
5961
<label for="city">City</label>
60-
<input type="text" id="city" name="city" />
62+
<input type="text" id="city" name="city" formControlName="city" />
6163
</div>
6264
</div>
6365
</fieldset>
@@ -68,7 +70,7 @@ <h2>Welcome on board!</h2>
6870
<div class="control-row">
6971
<div class="control">
7072
<label for="role">What best describes your role?</label>
71-
<select id="role" name="role">
73+
<select id="role" name="role" formControlName="role">
7274
<option value="student">Student</option>
7375
<option value="teacher">Teacher</option>
7476
<option value="employee">Employee</option>
@@ -83,50 +85,50 @@ <h2>Welcome on board!</h2>
8385
<div class="control-row">
8486
<label for="role"> Your Gender </label>
8587
<div class="radio">
86-
<input type="radio" id="male" name="genger" value="male" />
88+
<input type="radio" id="male" name="gender" value="male" formControlName="gender" />
8789
<label for="html">Male</label>
8890
</div>
8991
<div class="radio">
90-
<input type="radio" id="female" name="genger" value="female" />
92+
<input type="radio" id="female" name="gender" value="female" formControlName="gender" />
9193
<label for="css">Female</label>
9294
</div>
9395
<div class="radio">
94-
<input type="radio" id="others" name="genger" value="others" />
96+
<input type="radio" id="others" name="gender" value="others" formControlName="gender" />
9597
<label for="html">Not Ready To Tell</label>
9698
</div>
9799
</div>
98100

99101
<br />
100102

101-
<fieldset>
103+
<fieldset formArrayName="source">
102104
<legend>How did you find us?</legend>
103105
<div class="control">
104-
<input type="checkbox" id="google" name="acquisition" value="google" />
106+
<input type="checkbox" id="google" name="acquisition" value="google" formControlName="0" />
105107
<label for="google">Google</label>
106108
</div>
107109

108110
<div class="control">
109-
<input type="checkbox" id="friend" name="acquisition" value="friend" />
111+
<input type="checkbox" id="friend" name="acquisition" value="friend" formControlName="1" />
110112
<label for="friend">Referred by friend</label>
111113
</div>
112114

113115
<div class="control">
114-
<input type="checkbox" id="other" name="acquisition" value="other" />
116+
<input type="checkbox" id="other" name="acquisition" value="other" formControlName="2" />
115117
<label for="other">Other</label>
116118
</div>
117119
</fieldset>
118120

119121
<div class="control-row">
120122
<div class="control">
121123
<label for="terms-and-conditions">
122-
<input type="checkbox" id="terms-and-conditions" name="terms" />
124+
<input type="checkbox" id="terms-and-conditions" name="terms" formControlName="agree" />
123125
I agree to the terms and conditions
124126
</label>
125127
</div>
126128
</div>
127129

128130
<p class="form-actions">
129-
<button type="reset" class="button button-flat">Reset</button>
131+
<button type="reset" class="button button-flat" (click)="onReset()">Reset</button>
130132
<button type="submit" class="button">Sign up</button>
131133
</p>
132134
</form>
Lines changed: 56 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,64 @@
11
import { Component } from '@angular/core';
2+
import { FormArray, FormControl, FormGroup, ReactiveFormsModule, Validators } from '@angular/forms';
23

34
@Component({
45
selector: 'app-signup',
56
standalone: true,
7+
imports: [ReactiveFormsModule],
68
templateUrl: './signup.component.html',
79
styleUrl: './signup.component.css',
810
})
9-
export class SignupComponent {}
11+
export class SignupComponent {
12+
protected readonly minPassLen = 5;
13+
14+
formObj = new FormGroup({
15+
email: new FormControl('', {
16+
validators: [Validators.required, Validators.email],
17+
}),
18+
passwords: new FormGroup({
19+
password: new FormControl('', {
20+
validators: [Validators.required, Validators.minLength(this.minPassLen)],
21+
}),
22+
confirmPass: new FormControl('', {
23+
validators: [Validators.required, Validators.minLength(this.minPassLen)],
24+
}),
25+
}),
26+
firstName: new FormControl('', {
27+
validators: [Validators.required],
28+
}),
29+
lastName: new FormControl('', {
30+
validators: [Validators.required],
31+
}),
32+
address: new FormGroup({
33+
street: new FormControl('', {
34+
validators: [Validators.required],
35+
}),
36+
number: new FormControl('', {
37+
validators: [Validators.required],
38+
}),
39+
postal: new FormControl('', {
40+
validators: [Validators.required],
41+
}),
42+
city: new FormControl('', {
43+
validators: [Validators.required],
44+
}),
45+
}),
46+
role: new FormControl<'student' | 'teacher' | 'employee' | 'founder' | 'other'>('student', {
47+
validators: [Validators.required],
48+
}),
49+
gender: new FormControl<'male' | 'female' | 'others' | null>(null, {
50+
validators: [Validators.required],
51+
}),
52+
source: new FormArray([new FormControl(false), new FormControl(false), new FormControl(false)]),
53+
agree: new FormControl(false, { validators: [Validators.required] }),
54+
});
55+
56+
onSubmit() {
57+
console.log('this.formObj => ', this.formObj);
58+
console.log('this.formObj.value => ', this.formObj.value);
59+
}
60+
61+
onReset() {
62+
this.formObj.reset();
63+
}
64+
}

0 commit comments

Comments
 (0)