From 21e39a41e5410c6bba1b55d9c48672aef944523f Mon Sep 17 00:00:00 2001 From: Alex ORLUC <alex.orluc@maarch.org> Date: Tue, 17 Jul 2018 13:46:27 +0200 Subject: [PATCH] FIX formgroup validators --- .../app/password-modification.component.ts | 15 +++++++++++---- .../js/angular/app/profile.component.ts | 10 +++++++--- 2 files changed, 18 insertions(+), 7 deletions(-) diff --git a/apps/maarch_entreprise/js/angular/app/password-modification.component.ts b/apps/maarch_entreprise/js/angular/app/password-modification.component.ts index ab564a04ea6..6372627a547 100644 --- a/apps/maarch_entreprise/js/angular/app/password-modification.component.ts +++ b/apps/maarch_entreprise/js/angular/app/password-modification.component.ts @@ -97,14 +97,18 @@ export class PasswordModificationComponent implements OnInit { this.coreUrl = angularGlobals.coreUrl; this.http.get(this.coreUrl + 'rest/passwordRules') .subscribe((data: any) => { + let valArr : ValidatorFn[] = []; let ruleTextArr: String[] = []; + + valArr.push(Validators.required); + data.rules.forEach((rule: any) => { if (rule.label == 'minLength') { this.passwordRules.minLength.enabled = rule.enabled; this.passwordRules.minLength.value = rule.value; - this.firstFormGroup.controls["newPasswordCtrl"].setValidators([Validators.minLength(this.passwordRules.minLength.value)]) + valArr.push(Validators.minLength(this.passwordRules.minLength.value)); if (rule.enabled) { ruleTextArr.push(rule.value + ' ' + this.lang['password' + rule.label]); } @@ -113,6 +117,7 @@ export class PasswordModificationComponent implements OnInit { } else if (rule.label == 'complexityUpper') { this.passwordRules.complexityUpper.enabled = rule.enabled; this.passwordRules.complexityUpper.value = rule.value; + valArr.push(this.regexValidator(new RegExp('[A-Z]'), { 'complexityUpper': '' })); if (rule.enabled) { ruleTextArr.push(this.lang['password' + rule.label]); } @@ -121,6 +126,7 @@ export class PasswordModificationComponent implements OnInit { } else if (rule.label == 'complexityNumber') { this.passwordRules.complexityNumber.enabled = rule.enabled; this.passwordRules.complexityNumber.value = rule.value; + valArr.push(this.regexValidator(new RegExp('[0-9]'), { 'complexityNumber': '' })); if (rule.enabled) { ruleTextArr.push(this.lang['password' + rule.label]); } @@ -129,6 +135,7 @@ export class PasswordModificationComponent implements OnInit { } else if (rule.label == 'complexitySpecial') { this.passwordRules.complexitySpecial.enabled = rule.enabled; this.passwordRules.complexitySpecial.value = rule.value; + valArr.push(this.regexValidator(new RegExp('[^A-Za-z0-9]'), { 'complexitySpecial': '' })); if (rule.enabled) { ruleTextArr.push(this.lang['password' + rule.label]); } @@ -145,17 +152,16 @@ export class PasswordModificationComponent implements OnInit { ruleTextArr.push(this.lang['passwordhistoryLastUseDesc'] + ' ' + rule.value + ' ' + this.lang['passwordhistoryLastUseDesc2']); } } - }); this.ruleText = ruleTextArr.join(', '); + this.firstFormGroup.controls["newPasswordCtrl"].setValidators(valArr); }, (err) => { this.notify.error(err.error.errors); }); this.firstFormGroup = this._formBuilder.group({ newPasswordCtrl: [ - '', - Validators.compose([Validators.minLength(1), this.regexValidator(new RegExp('[A-Z]'), { 'complexityUpper': '' }), this.regexValidator(new RegExp('[0-9]'), { 'complexityNumber': '' }), this.regexValidator(new RegExp('[^A-Za-z0-9]'), { 'complexitySpecial': '' })]) + '' ], retypePasswordCtrl: [ '', @@ -182,6 +188,7 @@ export class PasswordModificationComponent implements OnInit { } getErrorMessage() { + console.log(this.firstFormGroup.controls['newPasswordCtrl'].errors); if (this.firstFormGroup.controls['newPasswordCtrl'].hasError('required')) { return this.lang.requiredField + ' !'; diff --git a/apps/maarch_entreprise/js/angular/app/profile.component.ts b/apps/maarch_entreprise/js/angular/app/profile.component.ts index 2297d5e7cc7..14584f812e7 100755 --- a/apps/maarch_entreprise/js/angular/app/profile.component.ts +++ b/apps/maarch_entreprise/js/angular/app/profile.component.ts @@ -745,12 +745,13 @@ export class ProfileComponent extends AutoCompletePlugin implements OnInit { changePasswd() { this.http.get(this.coreUrl + 'rest/passwordRules') .subscribe((data: any) => { + let valArr : ValidatorFn[] = []; let ruleTextArr: String[] = []; data.rules.forEach((rule: any) => { if (rule.label == 'minLength') { this.passwordRules.minLength.enabled = rule.enabled; this.passwordRules.minLength.value = rule.value; - this.firstFormGroup.controls["newPasswordCtrl"].setValidators([Validators.minLength(this.passwordRules.minLength.value)]) + valArr.push(Validators.minLength(this.passwordRules.minLength.value)); if (rule.enabled) { ruleTextArr.push(rule.value + ' ' + this.lang['password' + rule.label]); } @@ -759,6 +760,7 @@ export class ProfileComponent extends AutoCompletePlugin implements OnInit { } else if (rule.label == 'complexityUpper') { this.passwordRules.complexityUpper.enabled = rule.enabled; this.passwordRules.complexityUpper.value = rule.value; + valArr.push(this.regexValidator(new RegExp('[A-Z]'), { 'complexityUpper': '' })); if (rule.enabled) { ruleTextArr.push(this.lang['password' + rule.label]); } @@ -767,6 +769,7 @@ export class ProfileComponent extends AutoCompletePlugin implements OnInit { } else if (rule.label == 'complexityNumber') { this.passwordRules.complexityNumber.enabled = rule.enabled; this.passwordRules.complexityNumber.value = rule.value; + valArr.push(this.regexValidator(new RegExp('[0-9]'), { 'complexityNumber': '' })); if (rule.enabled) { ruleTextArr.push(this.lang['password' + rule.label]); } @@ -775,6 +778,7 @@ export class ProfileComponent extends AutoCompletePlugin implements OnInit { } else if (rule.label == 'complexitySpecial') { this.passwordRules.complexitySpecial.enabled = rule.enabled; this.passwordRules.complexitySpecial.value = rule.value; + valArr.push(this.regexValidator(new RegExp('[^A-Za-z0-9]'), { 'complexitySpecial': '' })); if (rule.enabled) { ruleTextArr.push(this.lang['password' + rule.label]); } @@ -794,14 +798,14 @@ export class ProfileComponent extends AutoCompletePlugin implements OnInit { }); this.ruleText = ruleTextArr.join(', '); + this.firstFormGroup.controls["newPasswordCtrl"].setValidators(valArr); }, (err) => { this.notify.error(err.error.errors); }); this.firstFormGroup = this._formBuilder.group({ newPasswordCtrl: [ - '', - Validators.compose([Validators.minLength(1), this.regexValidator(new RegExp('[A-Z]'), { 'complexityUpper': '' }), this.regexValidator(new RegExp('[0-9]'), { 'complexityNumber': '' }), this.regexValidator(new RegExp('[^A-Za-z0-9]'), { 'complexitySpecial': '' })]) + '' ], retypePasswordCtrl: [ '', -- GitLab