From 16ccb9bdcbb23f16a1bf2ef6bea459255dd516f8 Mon Sep 17 00:00:00 2001 From: Anand raja Date: Fri, 4 Oct 2024 08:08:11 +0000 Subject: [PATCH 1/3] refactoring --- src/app/reactive/login/login.component.ts | 10 +++++++++- src/app/reactive/signup/signup.component.html | 16 +++++++++------ src/app/reactive/signup/signup.component.ts | 20 ++++++++++++++++++- 3 files changed, 38 insertions(+), 8 deletions(-) diff --git a/src/app/reactive/login/login.component.ts b/src/app/reactive/login/login.component.ts index 09a5d9d..3b8a99a 100644 --- a/src/app/reactive/login/login.component.ts +++ b/src/app/reactive/login/login.component.ts @@ -8,7 +8,7 @@ const reqSymbol = '?'; const reservedEmail = 'ar@example.com'; function isPasswordContainReqChar(control: AbstractControl) { - if (control.value.includes(reqSymbol)) { + if (control.value?.includes(reqSymbol)) { return null; } else { return { PassNotHaveSymbol: true }; @@ -86,6 +86,14 @@ export class LoginComponent2 implements OnInit { } onSubmit() { + if (this.formObj.invalid) { + return; + } + // this.formObj.controls.emailField.addValidators + console.log('this.formObj.controls.emailField => ', this.formObj.controls.emailField); + console.log('this.formObj.value => ', this.formObj.value); + + this.formObj.reset(); } } diff --git a/src/app/reactive/signup/signup.component.html b/src/app/reactive/signup/signup.component.html index e8949b4..d823d05 100644 --- a/src/app/reactive/signup/signup.component.html +++ b/src/app/reactive/signup/signup.component.html @@ -1,21 +1,25 @@ -
+

Welcome on board!

We just need a little bit of data from you to get you started 🚀

- +
-
+
- +
- +
@@ -126,7 +130,7 @@

Welcome on board!

- +

diff --git a/src/app/reactive/signup/signup.component.ts b/src/app/reactive/signup/signup.component.ts index 94d9915..daa76f9 100644 --- a/src/app/reactive/signup/signup.component.ts +++ b/src/app/reactive/signup/signup.component.ts @@ -1,9 +1,27 @@ import { Component } from '@angular/core'; +import { FormControl, FormGroup, ReactiveFormsModule } from '@angular/forms'; @Component({ selector: 'app-signup', standalone: true, + imports: [ReactiveFormsModule], templateUrl: './signup.component.html', styleUrl: './signup.component.css', }) -export class SignupComponent {} +export class SignupComponent { + formObj = new FormGroup({ + email: new FormControl(''), + passwords: new FormGroup({ + password: new FormControl(''), + confirmPass: new FormControl(''), + }), + }); + + onSubmit() { + console.log(this.formObj.value); + } + + onReset() { + this.formObj.reset(); + } +} From 77e19c405a444c19c15032212a01b8554b183d54 Mon Sep 17 00:00:00 2001 From: Anand raja Date: Fri, 4 Oct 2024 08:37:45 +0000 Subject: [PATCH 2/3] complex form started --- src/app/reactive/signup/signup.component.html | 24 ++++++------- src/app/reactive/signup/signup.component.ts | 36 ++++++++++++++++--- 2 files changed, 43 insertions(+), 17 deletions(-) diff --git a/src/app/reactive/signup/signup.component.html b/src/app/reactive/signup/signup.component.html index d823d05..7d7f489 100644 --- a/src/app/reactive/signup/signup.component.html +++ b/src/app/reactive/signup/signup.component.html @@ -4,17 +4,15 @@

Welcome on board!

- + +
- + +
@@ -29,39 +27,39 @@

Welcome on board!

- +
- +
-
+
Your Address
- +
- +
- +
- +
diff --git a/src/app/reactive/signup/signup.component.ts b/src/app/reactive/signup/signup.component.ts index daa76f9..63d9306 100644 --- a/src/app/reactive/signup/signup.component.ts +++ b/src/app/reactive/signup/signup.component.ts @@ -1,5 +1,5 @@ import { Component } from '@angular/core'; -import { FormControl, FormGroup, ReactiveFormsModule } from '@angular/forms'; +import { FormControl, FormGroup, ReactiveFormsModule, Validators } from '@angular/forms'; @Component({ selector: 'app-signup', @@ -9,11 +9,39 @@ import { FormControl, FormGroup, ReactiveFormsModule } from '@angular/forms'; styleUrl: './signup.component.css', }) export class SignupComponent { + protected readonly minPassLen = 5; + formObj = new FormGroup({ - email: new FormControl(''), + email: new FormControl('', { + validators: [Validators.required, Validators.email], + }), passwords: new FormGroup({ - password: new FormControl(''), - confirmPass: new FormControl(''), + password: new FormControl('', { + validators: [Validators.required, Validators.minLength(this.minPassLen)], + }), + confirmPass: new FormControl('', { + validators: [Validators.required, Validators.minLength(this.minPassLen)], + }), + }), + firstName: new FormControl('', { + validators: [Validators.required], + }), + lastName: new FormControl('', { + validators: [Validators.required], + }), + address: new FormGroup({ + street: new FormControl('', { + validators: [Validators.required], + }), + number: new FormControl('', { + validators: [Validators.required], + }), + postal: new FormControl('', { + validators: [Validators.required], + }), + city: new FormControl('', { + validators: [Validators.required], + }), }), }); From 7183b776772ef8876f8f0f77129cf549da4dcfd8 Mon Sep 17 00:00:00 2001 From: Anand raja Date: Fri, 4 Oct 2024 10:40:52 +0000 Subject: [PATCH 3/3] form array completed --- src/app/reactive/signup/signup.component.html | 20 +++++++++---------- src/app/reactive/signup/signup.component.ts | 13 ++++++++++-- 2 files changed, 21 insertions(+), 12 deletions(-) diff --git a/src/app/reactive/signup/signup.component.html b/src/app/reactive/signup/signup.component.html index 7d7f489..abcdb5b 100644 --- a/src/app/reactive/signup/signup.component.html +++ b/src/app/reactive/signup/signup.component.html @@ -42,7 +42,7 @@

Welcome on board!

- +
@@ -70,7 +70,7 @@

Welcome on board!

- @@ -85,35 +85,35 @@

Welcome on board!

- +
- +
- +

-
+
How did you find us?
- +
- +
- +
@@ -121,7 +121,7 @@

Welcome on board!

diff --git a/src/app/reactive/signup/signup.component.ts b/src/app/reactive/signup/signup.component.ts index 63d9306..ca5080f 100644 --- a/src/app/reactive/signup/signup.component.ts +++ b/src/app/reactive/signup/signup.component.ts @@ -1,5 +1,5 @@ import { Component } from '@angular/core'; -import { FormControl, FormGroup, ReactiveFormsModule, Validators } from '@angular/forms'; +import { FormArray, FormControl, FormGroup, ReactiveFormsModule, Validators } from '@angular/forms'; @Component({ selector: 'app-signup', @@ -43,10 +43,19 @@ export class SignupComponent { validators: [Validators.required], }), }), + role: new FormControl<'student' | 'teacher' | 'employee' | 'founder' | 'other'>('student', { + validators: [Validators.required], + }), + gender: new FormControl<'male' | 'female' | 'others' | null>(null, { + validators: [Validators.required], + }), + source: new FormArray([new FormControl(false), new FormControl(false), new FormControl(false)]), + agree: new FormControl(false, { validators: [Validators.required] }), }); onSubmit() { - console.log(this.formObj.value); + console.log('this.formObj => ', this.formObj); + console.log('this.formObj.value => ', this.formObj.value); } onReset() {