From 0d7a030960116e6635f5dacd9c007c992dbd9d9d Mon Sep 17 00:00:00 2001 From: Alexandre Vryghem Date: Wed, 25 Jan 2023 17:46:21 +0100 Subject: [PATCH] 98863: Fixed typo and added domain name validation on input field --- .../register-email-form.component.ts | 23 ++++++++++++++----- src/assets/i18n/en.json5 | 2 +- 2 files changed, 18 insertions(+), 7 deletions(-) diff --git a/src/app/register-email-form/register-email-form.component.ts b/src/app/register-email-form/register-email-form.component.ts index dc742d6760..9269a0cb2d 100644 --- a/src/app/register-email-form/register-email-form.component.ts +++ b/src/app/register-email-form/register-email-form.component.ts @@ -3,11 +3,12 @@ import {EpersonRegistrationService} from '../core/data/eperson-registration.serv import {NotificationsService} from '../shared/notifications/notifications.service'; import {TranslateService} from '@ngx-translate/core'; import {Router} from '@angular/router'; -import {FormBuilder, FormControl, FormGroup, Validators} from '@angular/forms'; +import { FormBuilder, FormControl, FormGroup, Validators, ValidatorFn } from '@angular/forms'; import {Registration} from '../core/shared/registration.model'; import {RemoteData} from '../core/data/remote-data'; import {ConfigurationDataService} from '../core/data/configuration-data.service'; import {getAllCompletedRemoteData} from '../core/shared/operators'; +import { ConfigurationProperty } from '../core/shared/configuration-property.model'; @Component({ selector: 'ds-register-email-form', @@ -48,22 +49,32 @@ export class RegisterEmailFormComponent implements OnInit { } ngOnInit(): void { + const validators: ValidatorFn[] = [ + Validators.required, + Validators.pattern('^[a-z0-9._%+-]+@[a-z0-9.-]+\.[a-z]{2,4}$') + ]; this.form = this.formBuilder.group({ email: new FormControl('', { - validators: [Validators.required, - Validators.pattern('^[a-z0-9._%+-]+@[a-z0-9.-]+\.[a-z]{2,4}$') - ], + validators: validators, }) }); this.validMailDomains = []; this.configurationService.findByPropertyName('authentication-password.domain.valid') .pipe(getAllCompletedRemoteData()) - .subscribe((remoteData) => { + .subscribe((remoteData: RemoteData) => { + if (remoteData.payload) { for (const remoteValue of remoteData.payload.values) { this.validMailDomains.push(remoteValue); + if (this.validMailDomains.length !== 0 && this.MESSAGE_PREFIX === 'register-page.registration') { + this.form.get('email').setValidators([ + ...validators, + Validators.pattern(this.validMailDomains.map((domain: string) => '(^.*@' + domain.replace(new RegExp('\\.', 'g'), '\\.') + '$)').join('|')), + ]); + this.form.updateValueAndValidity(); + } } } - ); + }); } /** diff --git a/src/assets/i18n/en.json5 b/src/assets/i18n/en.json5 index 8737bed9b3..511d923f7d 100644 --- a/src/assets/i18n/en.json5 +++ b/src/assets/i18n/en.json5 @@ -3027,7 +3027,7 @@ "register-page.registration.error.content": "An error occured when registering the following email address: {{ email }}", - "register-page.registration.error.maildomain": "this email address is not on the list of domains who can register. Allowed domains are {{ domains }}", + "register-page.registration.error.maildomain": "This email address is not on the list of domains who can register. Allowed domains are {{ domains }}", "register-page.registration.info.maildomain": "Accounts can be registered for mail addresses of the domains",