Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add max per option validator and onkeyon for votes input fields #4492

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -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 = `rangeError`): 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;
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,11 @@ <h2 class="poll-preview-title">
type="number"
[errorStateMatcher]="parentErrorStateMatcher"
/>
@if (contentForm.controls['votes_amount'].hasError('rangeErrorMaxPerOption')) {
<mat-error>
{{ 'Max votes per option cannot be greater than max votes.' | translate }}
</mat-error>
}
</mat-form-field>
</ng-container>
</div>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -474,7 +474,8 @@ 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`)
this.enoughPollOptionsAvailable(`min_votes_amount`, `max_votes_per_option`),
isNumberRange(`max_votes_per_option`, `max_votes_amount`, `rangeErrorMaxPerOption`)
]
}
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -318,6 +318,7 @@ <h4 class="poll-delegation-title">
[formControl]="getFormControl(option.id)"
max="{{ poll.max_votes_per_option }}"
(change)="saveMultipleVotes(option.id, $event, delegation)"
(keyup)="saveMultipleVotes(option.id, $event, delegation)"
/>
<mat-error>
{{ getErrorInVoteEntry(option.id) }}
Expand Down
Loading