Skip to content

Commit

Permalink
Add validators to email and default_vote_weight in some dialog (#2911)
Browse files Browse the repository at this point in the history
  • Loading branch information
reiterl authored Nov 3, 2023
1 parent 77bfe4e commit 5fd48da
Show file tree
Hide file tree
Showing 8 changed files with 39 additions and 7 deletions.
19 changes: 19 additions & 0 deletions client/src/app/infrastructure/utils/validators/email.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { AbstractControl, ValidationErrors, ValidatorFn } from '@angular/forms';

const EMAIL_VALIDATION_REGEX = /[A-Z0-9._+\-ÄÖÜ]+@[A-Z0-9.\-ÄÖÜ]+\.[A-ZÄÖÜ]{2,}/i;

export function createEmailValidator(): ValidatorFn {
return (control: AbstractControl): ValidationErrors | null => {
const value = control.value;

if (!value) {
return null;
}

const validEmail = EMAIL_VALIDATION_REGEX.test(value);
if (!validEmail) {
return { email: `Please enter a valid email address` };
}
return null;
};
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import {
} from '@angular/core';
import { UntypedFormBuilder, UntypedFormGroup, ValidatorFn, Validators } from '@angular/forms';
import { Subscription } from 'rxjs';
import { createEmailValidator } from 'src/app/infrastructure/utils/validators/email';
import { OperatorService } from 'src/app/site/services/operator.service';
import { BaseUiComponent } from 'src/app/ui/base/base-ui-component';

Expand Down Expand Up @@ -294,7 +295,7 @@ export class UserDetailViewComponent extends BaseUiComponent implements OnInit,
first_name: [``],
last_name: [``],
gender: [``],
email: [``],
email: [``, [createEmailValidator()]],
last_email_sent: [``],
default_password: [``],
is_active: [true],
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { Component, OnInit } from '@angular/core';
import { UntypedFormBuilder, UntypedFormGroup, Validators } from '@angular/forms';
import { TranslateService } from '@ngx-translate/core';
import { createEmailValidator } from 'src/app/infrastructure/utils/validators/email';
import { BaseComponent } from 'src/app/site/base/base.component';
import { ComponentServiceCollectorService } from 'src/app/site/services/component-service-collector.service';
import { UserControllerService } from 'src/app/site/services/user-controller.service';
Expand Down Expand Up @@ -33,7 +34,7 @@ export class ResetPasswordComponent extends BaseComponent implements OnInit {
) {
super(componentServiceCollector, translate);
this.resetPasswordForm = formBuilder.group({
email: [``, [Validators.required, Validators.email]]
email: [``, [Validators.required, createEmailValidator()]]
});
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { BehaviorSubject, distinctUntilChanged, Observable } from 'rxjs';
import { Id } from 'src/app/domain/definitions/key-types';
import { User } from 'src/app/domain/models/users/user';
import { SearchUsersPresenterService } from 'src/app/gateways/presenter/search-users-presenter.service';
import { createEmailValidator } from 'src/app/infrastructure/utils/validators/email';
import { OneOfValidator, UserDetailViewComponent } from 'src/app/site/modules/user-components';
import { BaseMeetingComponent } from 'src/app/site/pages/meetings/base/base-meeting.component';
import { ViewGroup } from 'src/app/site/pages/meetings/pages/participants';
Expand Down Expand Up @@ -152,7 +153,7 @@ export class ParticipantCreateWizardComponent extends BaseMeetingComponent imple
username: [``],
first_name: [``],
last_name: [``],
email: [``]
email: [``, createEmailValidator()]
},
{
validators: [OneOfValidator.validation([`username`, `first_name`, `last_name`, `email`], `name`)]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import {
PollTypeVerbose
} from 'src/app/domain/models/poll/poll-constants';
import { ObjectReplaceKeysConfig } from 'src/app/infrastructure/utils';
import { createEmailValidator } from 'src/app/infrastructure/utils/validators/email';

import { OrganizationSettingsService } from '../../../organization/services/organization-settings.service';
import { AssignmentPollMethodVerbose } from '../../pages/assignments/modules/assignment-poll/definitions';
Expand Down Expand Up @@ -905,7 +906,7 @@ export const meetingSettings: SettingsGroup[] = fillInSettingsDefaults([
key: `users_email_replyto`,
label: _(`Reply address`),
type: `email`,
validators: [Validators.email]
validators: [createEmailValidator()]
},
{
key: `users_email_subject`,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,9 @@ <h2>{{ 'Organization specific information' | translate }}</h2>
placeholder="{{ 'Default vote weight' | translate }}"
formControlName="default_vote_weight"
/>
<mat-error *ngIf="isDefaultVoteWeightError" translate>
Please select a vote weight bigger than zero.
</mat-error>
</mat-form-field>
</div>

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { KeyValue } from '@angular/common';
import { Component, OnInit, ViewChild } from '@angular/core';
import { Validators } from '@angular/forms';
import { ActivatedRoute } from '@angular/router';
import { marker as _ } from '@colsen1991/ngx-translate-extract-marker';
import { TranslateService } from '@ngx-translate/core';
Expand Down Expand Up @@ -61,7 +62,7 @@ export class AccountDetailComponent extends BaseComponent implements OnInit {
public readonly additionalFormControls = {
default_structure_level: [``],
default_number: [``],
default_vote_weight: [``],
default_vote_weight: [``, Validators.min(0.000001)],
organization_management_level: [],
committee_management_ids: []
};
Expand Down Expand Up @@ -203,6 +204,10 @@ export class AccountDetailComponent extends BaseComponent implements OnInit {
return Object.keys(item).length;
}

public get isDefaultVoteWeightError(): boolean {
return this.personalInfoFormValue.default_vote_weight < 0.000001;
}

private generateParticipationTableData(): void {
const tableData: ParticipationTableData = this.user.committees.mapToObject(item => {
return {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import { ChangeDetectionStrategy, Component } from '@angular/core';
import { UntypedFormBuilder, UntypedFormGroup, Validators } from '@angular/forms';
import { UntypedFormBuilder, UntypedFormGroup } from '@angular/forms';
import { marker as _ } from '@colsen1991/ngx-translate-extract-marker';
import { TranslateService } from '@ngx-translate/core';
import { availableTranslations } from 'src/app/domain/definitions/languages';
import { objectToFormattedString } from 'src/app/infrastructure/utils';
import { createEmailValidator } from 'src/app/infrastructure/utils/validators/email';
import { BaseComponent } from 'src/app/site/base/base.component';
import { ORGANIZATION_ID } from 'src/app/site/pages/organization/services/organization.service';
import { OrganizationControllerService } from 'src/app/site/pages/organization/services/organization-controller.service';
Expand Down Expand Up @@ -70,7 +71,7 @@ export class OrganizationSettingsComponent extends BaseComponent {
privacy_policy: [this._currentOrgaSettings.privacy_policy],
login_text: [this._currentOrgaSettings.login_text],
users_email_body: [this._currentOrgaSettings.users_email_body],
users_email_replyto: [this._currentOrgaSettings.users_email_replyto, [Validators.email]],
users_email_replyto: [this._currentOrgaSettings.users_email_replyto, [createEmailValidator()]],
users_email_sender: [this._currentOrgaSettings.users_email_sender],
users_email_subject: [this._currentOrgaSettings.users_email_subject],
default_language: [this._currentOrgaSettings.default_language]
Expand Down

0 comments on commit 5fd48da

Please sign in to comment.