Browse Source

UI: Fixed time unit input validators

pull/14824/head
ArtemDzhereleiko 7 months ago
parent
commit
2ab5e01ddf
  1. 2
      ui-ngx/src/app/modules/home/pages/admin/two-factor-auth-settings.component.ts
  2. 68
      ui-ngx/src/app/shared/components/time-unit-input.component.ts

2
ui-ngx/src/app/modules/home/pages/admin/two-factor-auth-settings.component.ts

@ -69,6 +69,8 @@ export class TwoFactorAuthSettingsComponent extends PageComponent implements OnI
this.build2faSettingsForm();
this.twoFaService.getTwoFaSettings().subscribe((setting) => {
this.setAuthConfigFormValue(setting);
this.twoFaFormGroup.markAsUntouched();
this.twoFaFormGroup.markAsPristine();
});
}

68
ui-ngx/src/app/shared/components/time-unit-input.component.ts

@ -134,38 +134,13 @@ export class TimeUnitInputComponent implements ControlValueAccessor, Validator,
if (isDefinedAndNotNull(this.maxTime)) {
this.updatedAllowTimeUnitInterval(this.maxTime);
}
if (this.required || this.maxTime || isDefinedAndNotNull(this.minTime) || this.stepMultipleOf) {
const timeControl = this.timeInputForm.get('time');
const validators = [Validators.pattern(/^\d*$/)];
if (this.required) {
validators.push(Validators.required);
}
if (this.maxTime) {
validators.push((control: AbstractControl) =>
Validators.max(Math.floor(this.maxTime / this.timeIntervalsInSec.get(this.timeInputForm.get('timeUnit').value)))(control)
);
}
if (isDefinedAndNotNull(this.minTime)) {
this.minValueValidator = this.minValue;
validators.push((control: AbstractControl) =>
Validators.min(this.minValueValidator)(control)
);
}
if (isDefinedAndNotNull(this.stepMultipleOf) && this.stepMultipleOf > 0) {
validators.push(this.createStepMultipleOfValidator());
}
timeControl.setValidators(validators);
timeControl.updateValueAndValidity({ emitEvent: false });
}
this.refreshTimeValidators();
this.timeInputForm.get('timeUnit').valueChanges.pipe(
takeUntilDestroyed(this.destroyRef)
).subscribe(() => {
if (isDefinedAndNotNull(this.minTime)) {
this.minValueValidator = this.minValue;
}
this.refreshTimeValidators();
this.timeInputForm.get('time').updateValueAndValidity({onlySelf: true});
this.timeInputForm.get('time').markAsTouched({onlySelf: true});
});
@ -201,11 +176,10 @@ export class TimeUnitInputComponent implements ControlValueAccessor, Validator,
if (isDefinedAndNotNull(this.maxTime)) {
this.timeUnits = Object.values(TimeUnit).filter(item => item !== TimeUnit.MILLISECONDS) as TimeUnit[];
this.updatedAllowTimeUnitInterval(this.maxTime);
this.timeInputForm.get('time').updateValueAndValidity({emitEvent: false});
}
}
if (propName === 'minTime') {
this.minValueValidator = isDefinedAndNotNull(this.minTime) ? this.minValue : 0;
if (['minTime', 'maxTime', 'required', 'stepMultipleOf'].includes(propName)) {
this.refreshTimeValidators();
}
}
}
@ -312,4 +286,38 @@ export class TimeUnitInputComponent implements ControlValueAccessor, Validator,
}
}
private refreshTimeValidators() {
const timeControl = this.timeInputForm.get('time');
if (!timeControl) return;
const currentUnit = this.timeInputForm.get('timeUnit')?.value as TimeUnit;
if (!currentUnit) return;
const secondsInUnit = this.timeIntervalsInSec.get(currentUnit) ?? 1;
const validators: ValidatorFn[] = [Validators.pattern(/^\d*$/)];
if (this.required) {
validators.push(Validators.required);
}
if (isDefinedAndNotNull(this.minTime)) {
this.minValueValidator = Math.ceil(this.minTime / secondsInUnit);
validators.push(Validators.min(this.minValueValidator));
} else {
this.minValueValidator = 0;
validators.push(Validators.min(this.minValueValidator));
}
if (isDefinedAndNotNull(this.maxTime)) {
const maxInCurrentUnit = Math.floor(this.maxTime / secondsInUnit);
validators.push(Validators.max(maxInCurrentUnit));
}
if (isDefinedAndNotNull(this.stepMultipleOf) && this.stepMultipleOf > 0) {
validators.push(this.createStepMultipleOfValidator());
}
timeControl.setValidators(validators);
timeControl.updateValueAndValidity({ onlySelf: true, emitEvent: false });
}
}

Loading…
Cancel
Save