From 1a0e7d098928fb2b715b94b11034f6784420b6f7 Mon Sep 17 00:00:00 2001 From: Ludwig Reiter Date: Wed, 18 Dec 2024 08:17:21 +0100 Subject: [PATCH 1/3] Add max per option validator and update isNumberRange --- .../app/infrastructure/utils/validators/is-number-range.ts | 4 ++-- .../app/infrastructure/utils/validators/validators.spec.ts | 2 +- .../components/base-poll-form/base-poll-form.component.html | 5 +++++ .../components/base-poll-form/base-poll-form.component.ts | 5 +++-- 4 files changed, 11 insertions(+), 5 deletions(-) diff --git a/client/src/app/infrastructure/utils/validators/is-number-range.ts b/client/src/app/infrastructure/utils/validators/is-number-range.ts index 722259ce00..0c8b3795d7 100644 --- a/client/src/app/infrastructure/utils/validators/is-number-range.ts +++ b/client/src/app/infrastructure/utils/validators/is-number-range.ts @@ -1,11 +1,11 @@ import { AbstractControl, ValidatorFn } from '@angular/forms'; -export function isNumberRange(minCtrlName: string, maxCtrlName: string): ValidatorFn { +export function isNumberRange(minCtrlName: string, maxCtrlName: string, errorName: string): ValidatorFn { return (formControl: AbstractControl): { [key: string]: any } | null => { const min = formControl.get(minCtrlName)!.value; const max = formControl.get(maxCtrlName)!.value; if (+min > +max || Number.isNaN(+min) || Number.isNaN(+max)) { - return { rangeError: true }; + return { [errorName]: true }; } return null; }; diff --git a/client/src/app/infrastructure/utils/validators/validators.spec.ts b/client/src/app/infrastructure/utils/validators/validators.spec.ts index fcd16f8fc9..03ca2a6a6e 100644 --- a/client/src/app/infrastructure/utils/validators/validators.spec.ts +++ b/client/src/app/infrastructure/utils/validators/validators.spec.ts @@ -5,7 +5,7 @@ import { isUniqueAmong } from './is-unique-among'; describe(`utils: validators`, () => { describe(`isNumberRange function`, () => { - const validatorFn = isNumberRange(`min`, `max`); + const validatorFn = isNumberRange(`min`, `max`, `errorName`); it(`test with correct values`, () => { const group = new UntypedFormGroup( { diff --git a/client/src/app/site/pages/meetings/modules/poll/components/base-poll-form/base-poll-form.component.html b/client/src/app/site/pages/meetings/modules/poll/components/base-poll-form/base-poll-form.component.html index aba0ec4fbd..5f1098b195 100644 --- a/client/src/app/site/pages/meetings/modules/poll/components/base-poll-form/base-poll-form.component.html +++ b/client/src/app/site/pages/meetings/modules/poll/components/base-poll-form/base-poll-form.component.html @@ -116,6 +116,11 @@

type="number" [errorStateMatcher]="parentErrorStateMatcher" /> + @if (contentForm.controls['votes_amount'].hasError('rangeErrorMaxPerOption')) { + + {{ 'Max votes per option cannot be greater than max votes.' | translate }} + + } diff --git a/client/src/app/site/pages/meetings/modules/poll/components/base-poll-form/base-poll-form.component.ts b/client/src/app/site/pages/meetings/modules/poll/components/base-poll-form/base-poll-form.component.ts index 20211a5dac..982fdd2e06 100644 --- a/client/src/app/site/pages/meetings/modules/poll/components/base-poll-form/base-poll-form.component.ts +++ b/client/src/app/site/pages/meetings/modules/poll/components/base-poll-form/base-poll-form.component.ts @@ -473,8 +473,9 @@ export abstract class BasePollFormComponent extends BaseComponent implements OnI }, { validators: [ - isNumberRange(`min_votes_amount`, `max_votes_amount`), - this.enoughPollOptionsAvailable(`min_votes_amount`, `max_votes_per_option`) + isNumberRange(`min_votes_amount`, `max_votes_amount`, `rangeError`), + this.enoughPollOptionsAvailable(`min_votes_amount`, `max_votes_per_option`), + isNumberRange(`max_votes_per_option`, `max_votes_amount`, `rangeErrorMaxPerOption`) ] } ); From 0c0f8612ec233b0a1b5b6f480e1eca7cae2f2945 Mon Sep 17 00:00:00 2001 From: Ludwig Reiter Date: Wed, 18 Dec 2024 08:54:21 +0100 Subject: [PATCH 2/3] On key up also update the submitted values The on change is still needed, for mouse updates. The on key up checks without focus lose. --- .../poll/components/base-poll-vote/base-poll-vote.component.html | 1 + 1 file changed, 1 insertion(+) diff --git a/client/src/app/site/pages/meetings/modules/poll/components/base-poll-vote/base-poll-vote.component.html b/client/src/app/site/pages/meetings/modules/poll/components/base-poll-vote/base-poll-vote.component.html index 312f818c51..c84178a3b2 100644 --- a/client/src/app/site/pages/meetings/modules/poll/components/base-poll-vote/base-poll-vote.component.html +++ b/client/src/app/site/pages/meetings/modules/poll/components/base-poll-vote/base-poll-vote.component.html @@ -318,6 +318,7 @@

[formControl]="getFormControl(option.id)" max="{{ poll.max_votes_per_option }}" (change)="saveMultipleVotes(option.id, $event, delegation)" + (keyup)="saveMultipleVotes(option.id, $event, delegation)" /> {{ getErrorInVoteEntry(option.id) }} From 32d66368b2fc192664343fd2f3c2b37844b64d16 Mon Sep 17 00:00:00 2001 From: Ludwig Reiter Date: Thu, 19 Dec 2024 10:45:55 +0100 Subject: [PATCH 3/3] Set default for errorName in isNumberRange validator --- .../src/app/infrastructure/utils/validators/is-number-range.ts | 2 +- .../src/app/infrastructure/utils/validators/validators.spec.ts | 2 +- .../poll/components/base-poll-form/base-poll-form.component.ts | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/client/src/app/infrastructure/utils/validators/is-number-range.ts b/client/src/app/infrastructure/utils/validators/is-number-range.ts index 0c8b3795d7..5606d2b24a 100644 --- a/client/src/app/infrastructure/utils/validators/is-number-range.ts +++ b/client/src/app/infrastructure/utils/validators/is-number-range.ts @@ -1,6 +1,6 @@ import { AbstractControl, ValidatorFn } from '@angular/forms'; -export function isNumberRange(minCtrlName: string, maxCtrlName: string, errorName: string): ValidatorFn { +export function isNumberRange(minCtrlName: string, maxCtrlName: string, errorName: string = `rangeError`): ValidatorFn { return (formControl: AbstractControl): { [key: string]: any } | null => { const min = formControl.get(minCtrlName)!.value; const max = formControl.get(maxCtrlName)!.value; diff --git a/client/src/app/infrastructure/utils/validators/validators.spec.ts b/client/src/app/infrastructure/utils/validators/validators.spec.ts index 03ca2a6a6e..fcd16f8fc9 100644 --- a/client/src/app/infrastructure/utils/validators/validators.spec.ts +++ b/client/src/app/infrastructure/utils/validators/validators.spec.ts @@ -5,7 +5,7 @@ import { isUniqueAmong } from './is-unique-among'; describe(`utils: validators`, () => { describe(`isNumberRange function`, () => { - const validatorFn = isNumberRange(`min`, `max`, `errorName`); + const validatorFn = isNumberRange(`min`, `max`); it(`test with correct values`, () => { const group = new UntypedFormGroup( { diff --git a/client/src/app/site/pages/meetings/modules/poll/components/base-poll-form/base-poll-form.component.ts b/client/src/app/site/pages/meetings/modules/poll/components/base-poll-form/base-poll-form.component.ts index 982fdd2e06..b55940abad 100644 --- a/client/src/app/site/pages/meetings/modules/poll/components/base-poll-form/base-poll-form.component.ts +++ b/client/src/app/site/pages/meetings/modules/poll/components/base-poll-form/base-poll-form.component.ts @@ -473,7 +473,7 @@ export abstract class BasePollFormComponent extends BaseComponent implements OnI }, { validators: [ - isNumberRange(`min_votes_amount`, `max_votes_amount`, `rangeError`), + isNumberRange(`min_votes_amount`, `max_votes_amount`), this.enoughPollOptionsAvailable(`min_votes_amount`, `max_votes_per_option`), isNumberRange(`max_votes_per_option`, `max_votes_amount`, `rangeErrorMaxPerOption`) ]