From e46a39befe0e41c88fce3ecdb6c24d6ae98d5736 Mon Sep 17 00:00:00 2001 From: Gisela Lanciani Date: Mon, 28 Oct 2024 09:59:06 -0300 Subject: [PATCH 01/15] loginForm --- src/app/app.component.css | 2 +- src/app/login/login.component.html | 17 ++++------------- src/app/login/login.component.ts | 23 +++++++++-------------- 3 files changed, 14 insertions(+), 28 deletions(-) diff --git a/src/app/app.component.css b/src/app/app.component.css index 3516e92..fecf65e 100644 --- a/src/app/app.component.css +++ b/src/app/app.component.css @@ -1,5 +1,5 @@ ->>> body { +body { margin: 0; } diff --git a/src/app/login/login.component.html b/src/app/login/login.component.html index e4ed4db..ee484b8 100644 --- a/src/app/login/login.component.html +++ b/src/app/login/login.component.html @@ -1,27 +1,18 @@ Login - - - diff --git a/src/app/login/login.component.ts b/src/app/login/login.component.ts index 84905e8..391ad49 100644 --- a/src/app/login/login.component.ts +++ b/src/app/login/login.component.ts @@ -1,21 +1,16 @@ -import { Component, OnInit } from '@angular/core'; - +import { Component, OnInit } from "@angular/core"; +import { NgForm } from "@angular/forms"; @Component({ - selector: 'login', - templateUrl: './login.component.html', - styleUrls: ['./login.component.css'] + selector: "login", + templateUrl: "./login.component.html", + styleUrls: ["./login.component.css"], }) export class LoginComponent implements OnInit { + constructor() {} - - constructor() { - - + ngOnInit() {} + login(loginForm: NgForm) { + console.log(loginForm.value, loginForm.valid); } - - ngOnInit() { - - } - } From d942fb7a10b14c98ddde93ffa66d0625a1811c00 Mon Sep 17 00:00:00 2001 From: Gisela Lanciani Date: Mon, 28 Oct 2024 10:43:20 -0300 Subject: [PATCH 02/15] create ngModel --- src/app/login/login.component.html | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/src/app/login/login.component.html b/src/app/login/login.component.html index ee484b8..8ab0296 100644 --- a/src/app/login/login.component.html +++ b/src/app/login/login.component.html @@ -3,11 +3,15 @@ From 0549585f6b5a199793e5d54a5a8a50da56251259 Mon Sep 17 00:00:00 2001 From: Gisela Lanciani Date: Tue, 29 Oct 2024 11:50:56 -0300 Subject: [PATCH 06/15] one way and bidirectional data bingind --- src/app/login/login.component.html | 6 ++++-- src/app/login/login.component.ts | 10 ++++++++++ 2 files changed, 14 insertions(+), 2 deletions(-) diff --git a/src/app/login/login.component.html b/src/app/login/login.component.html index d5dff24..5214e3b 100644 --- a/src/app/login/login.component.html +++ b/src/app/login/login.component.html @@ -11,7 +11,7 @@ matInput type="email" name="email" - ngModel + [ngModel]="val.email" #email="ngModel" required email @@ -40,9 +40,11 @@ matInput type="password" name="password" - ngModel + [ngModel]="val.password" #password="ngModel" required + minlength="3" + maxlength="20" placeholder="Password" /> diff --git a/src/app/login/login.component.ts b/src/app/login/login.component.ts index 391ad49..ad8c193 100644 --- a/src/app/login/login.component.ts +++ b/src/app/login/login.component.ts @@ -1,5 +1,6 @@ import { Component, OnInit } from "@angular/core"; import { NgForm } from "@angular/forms"; +import { getMaxListeners } from "process"; @Component({ selector: "login", @@ -7,10 +8,19 @@ import { NgForm } from "@angular/forms"; styleUrls: ["./login.component.css"], }) export class LoginComponent implements OnInit { + + val = { + email: "hello@gmail.com", + password: "123456" + } constructor() {} ngOnInit() {} login(loginForm: NgForm) { console.log(loginForm.value, loginForm.valid); + + console.log(this.val); } + + } From 1241fda7b8ab5b64c082c627f93429302a3dbbcd Mon Sep 17 00:00:00 2001 From: Gisela Lanciani Date: Tue, 29 Oct 2024 12:17:45 -0300 Subject: [PATCH 07/15] create validators folder and function --- .../validators/password-strength.validator.ts | 28 +++++++++++++++++++ 1 file changed, 28 insertions(+) create mode 100644 src/app/validators/password-strength.validator.ts diff --git a/src/app/validators/password-strength.validator.ts b/src/app/validators/password-strength.validator.ts new file mode 100644 index 0000000..5f9175e --- /dev/null +++ b/src/app/validators/password-strength.validator.ts @@ -0,0 +1,28 @@ +import { AbstractControl, ValidationErrors, ValidatorFn } from "@angular/forms"; + +export function createPasswordStrengthValidator(): ValidatorFn { + return (control: AbstractControl) : ValidationErrors | null => { + + const value = control.value; + + if (!value) { + return null; + } + + const hasUpperCase = /[A-Z]+/.test(value); + + const hasLowerCase = /[a-z]+/.test(value); + + const hasNumeric = /[0-9]+/.test(value); + + const passwordValid = hasUpperCase && hasLowerCase && hasNumeric; + + return !passwordValid ? {passwordStrength: true} : null; + + + } + + } + + + From c4815cea88424f25ef5f22b1f0104c6afce1c0e7 Mon Sep 17 00:00:00 2001 From: Gisela Lanciani Date: Mon, 4 Nov 2024 11:27:43 -0300 Subject: [PATCH 08/15] implementing custom validators --- src/app/app.module.ts | 4 +++- .../directives/password-strength.directive.ts | 21 +++++++++++++++++++ src/app/login/login.component.html | 12 +++++++++-- src/app/login/login.component.ts | 2 +- 4 files changed, 35 insertions(+), 4 deletions(-) create mode 100644 src/app/directives/password-strength.directive.ts diff --git a/src/app/app.module.ts b/src/app/app.module.ts index 8410579..d6d87c9 100644 --- a/src/app/app.module.ts +++ b/src/app/app.module.ts @@ -41,6 +41,7 @@ import {CreateCourseStep1Component} from './create-course/create-course-step-1/c import {FileUploadComponent} from './file-upload/file-upload.component'; import {MatProgressBarModule} from '@angular/material/progress-bar'; import {LoginReactiveComponent} from './login-reactive/login-reactive.component'; +import { PasswordStrengthDirective } from './directives/password-strength.directive'; @NgModule({ declarations: [ AppComponent, @@ -56,7 +57,8 @@ import {LoginReactiveComponent} from './login-reactive/login-reactive.component' CreateCourseStep3Component, AddressFormComponent, FileUploadComponent, - LoginReactiveComponent + LoginReactiveComponent, + PasswordStrengthDirective ], bootstrap: [AppComponent], imports: [BrowserModule, BrowserAnimationsModule, diff --git a/src/app/directives/password-strength.directive.ts b/src/app/directives/password-strength.directive.ts new file mode 100644 index 0000000..12108ae --- /dev/null +++ b/src/app/directives/password-strength.directive.ts @@ -0,0 +1,21 @@ +import { Directive } from "@angular/core"; +import { AbstractControl, NG_VALIDATORS, ValidationErrors, Validator, Validators } from "@angular/forms"; +import { createPasswordStrengthValidator } from "../validators/password-strength.validator"; + + +@Directive({ + selector: '[passwordStrength]', + providers: [{ + provide: NG_VALIDATORS, + useExisting: PasswordStrengthDirective, + multi: true + }] + + +}) +export class PasswordStrengthDirective implements Validator { + + validate(control: AbstractControl): ValidationErrors | null{ + return createPasswordStrengthValidator()(control); + } +} diff --git a/src/app/login/login.component.html b/src/app/login/login.component.html index 5214e3b..0a88994 100644 --- a/src/app/login/login.component.html +++ b/src/app/login/login.component.html @@ -43,10 +43,18 @@ [ngModel]="val.password" #password="ngModel" required - minlength="3" - maxlength="20" + passwordStrength placeholder="Password" /> + + Your password must have lower case, upper case and numeric + characters. + + The password is mandatory - + +
+ {{ form.value | json }} +
+ +
+ {{ form.valid | json }} +
diff --git a/src/app/login-reactive/login-reactive.component.ts b/src/app/login-reactive/login-reactive.component.ts index c73b78a..fe286c6 100644 --- a/src/app/login-reactive/login-reactive.component.ts +++ b/src/app/login-reactive/login-reactive.component.ts @@ -1,21 +1,22 @@ -import { Component, OnInit } from '@angular/core'; - +import { Component, OnInit } from "@angular/core"; +import { FormControl, FormGroup, Validators } from "@angular/forms"; @Component({ - selector: 'login', - templateUrl: './login-reactive.component.html', - styleUrls: ['./login-reactive.component.css'] + selector: "login", + templateUrl: "./login-reactive.component.html", + styleUrls: ["./login-reactive.component.css"], }) export class LoginReactiveComponent implements OnInit { - - - constructor() { - - - } - - ngOnInit() { - - } - + form = new FormGroup({ + email: new FormControl("", { + validators: [Validators.required, Validators.email], + }), + password: new FormControl("",{ + validators: [Validators.required, Validators.minLength(8)], + }), + }); + + constructor() {} + + ngOnInit() {} } diff --git a/src/app/login/login.component.html b/src/app/login/login.component.html index b47b99e..c4104ae 100644 --- a/src/app/login/login.component.html +++ b/src/app/login/login.component.html @@ -1,5 +1,5 @@