SUPPORT-8971: add validation by idm

This commit is contained in:
Emir Suleimanov 2025-03-10 10:37:41 +03:00
parent 8fca640336
commit 8223fb4d9a
4 changed files with 69 additions and 75 deletions

View file

@ -17,5 +17,5 @@
"password_pattern_error": "Пароль должен содержать заглавные или прописные буквы и как минимум 1 цифру",
"show.client.errors": false,
"available_task.single_fetch": true,
"ervu_url": "https://ervu-dev.pgs.rtlabs.ru/service"
"ervu_url": "http://localhost:8081"
}

View file

@ -1,47 +1,15 @@
import {Behavior, TextField} from "@webbpm/base-package";
import {IdmService} from "../IdmService";
import {IdmTextFieldValidation} from "./IdmTextFieldValidation";
import {Observable} from "rxjs";
export class IdmLoginTextFieldValidation extends Behavior {
export class IdmLoginTextFieldValidation extends IdmTextFieldValidation {
protected ERROR_MESSAGE = "Пользователь с указанным логином уже существует";
private readonly LOGIN_ERROR_MESSAGE = "Пользователь с указанным логином уже существует";
private textField: TextField;
private idmService: IdmService;
protected textInput: any;
initialize(): void {
super.initialize();
this.textField = this.getScript(TextField);
this.idmService = this.injector.get(IdmService);
this.textInput = this.textField.getEl().nativeElement.querySelector('input');
this.textInput.addEventListener('blur', this.onBlurHandler.bind(this));
protected checkValueExists(value: string): Observable<boolean> {
return this.idmService.checkLoginExists(value);
}
private async onBlurHandler(event: FocusEvent): Promise<void> {
const inputValue : string = (event.target as HTMLInputElement).value;
if (!inputValue) return;
protected isValid(value: string): boolean {
let pattern = this.context.pattern;
if (pattern) {
let regExp = new RegExp(pattern);
if (!regExp.test(inputValue)) {
return;
}
}
this.idmService.checkLoginExists(inputValue).subscribe({
next: (isLoginExists) => {
if (isLoginExists) {
this.textField.addCustomValidationMessage(this.LOGIN_ERROR_MESSAGE);
this.textField.model.control.setErrors({loginExists: this.LOGIN_ERROR_MESSAGE});
}
else {
this.textField.removeCustomValidationMessage(this.LOGIN_ERROR_MESSAGE);
this.textField.model.control.setErrors(null);
}
},
error: (err) => {
console.error(`Ошибка проверки логина`, err);
}
});
return pattern ? new RegExp(pattern).test(value) : true;
}
}

View file

@ -1,40 +1,14 @@
import {Behavior, TextField} from "@webbpm/base-package";
import {IdmService} from "../IdmService";
import {IdmTextFieldValidation} from "./IdmTextFieldValidation";
import {Observable} from "rxjs";
export class IdmSnilsTextFieldValidation extends Behavior {
export class IdmSnilsTextFieldValidation extends IdmTextFieldValidation {
protected ERROR_MESSAGE = "Пользователь с указанным СНИЛС уже существует";
private readonly SNILS_ERROR_MESSAGE = "Пользователь с указанным СНИЛС уже существует";
private textField: TextField;
private idmService: IdmService;
protected textInput: any;
initialize(): void {
super.initialize();
this.textField = this.getScript(TextField);
this.idmService = this.injector.get(IdmService);
this.textInput = this.textField.getEl().nativeElement.querySelector('input');
this.textInput.addEventListener('blur', this.onBlurHandler.bind(this));
protected checkValueExists(value: string): Observable<boolean> {
return this.idmService.checkSnilsExists(value);
}
private async onBlurHandler(event: FocusEvent): Promise<void> {
const inputValue : string = (event.target as HTMLInputElement).value;
if (!inputValue) return;
const cleanedSnils = inputValue.replace(/[-\s]/g, '');
this.idmService.checkSnilsExists(cleanedSnils).subscribe({
next: (isSnilsExists) => {
if (isSnilsExists) {
this.textField.addCustomValidationMessage(this.SNILS_ERROR_MESSAGE);
this.textField.model.control.setErrors({ snilsExists: this.SNILS_ERROR_MESSAGE });
} else {
this.textField.removeCustomValidationMessage(this.SNILS_ERROR_MESSAGE);
this.textField.model.control.setErrors(null);
}
},
error: (err) => {
console.error(`Ошибка проверки снилса`, err);
}
});
protected cleanValue(value: string): string {
return value.replace(/[-\s]/g, '');
}
}

View file

@ -0,0 +1,52 @@
import {Behavior, TextField} from "@webbpm/base-package";
import {Observable} from "rxjs";
import {IdmService} from "../IdmService";
export abstract class IdmTextFieldValidation extends Behavior {
protected abstract ERROR_MESSAGE: string;
protected abstract checkValueExists(value: string): Observable<boolean>;
protected textField: TextField;
protected idmService: IdmService;
protected textInput: HTMLInputElement;
initialize(): void {
super.initialize();
this.textField = this.getScript(TextField);
this.idmService = this.injector.get(IdmService);
this.textInput = this.textField.getEl().nativeElement.querySelector('input');
this.textInput.addEventListener('blur', this.onBlurHandler.bind(this));
}
private async onBlurHandler(event: FocusEvent): Promise<void> {
const inputValue: string = (event.target as HTMLInputElement).value;
if (!inputValue) return;
const cleanedValue = this.cleanValue(inputValue);
if (!this.isValid(cleanedValue)) return;
this.checkValueExists(cleanedValue).subscribe({
next: (exists) => {
if (exists) {
this.textField.addCustomValidationMessage(this.ERROR_MESSAGE);
this.textField.model.control.setErrors({ exists: this.ERROR_MESSAGE });
} else {
this.textField.removeCustomValidationMessage(this.ERROR_MESSAGE);
this.textField.model.control.setErrors(null);
}
},
error: (err) => {
console.error(`Ошибка проверки: ${err}`);
}
});
}
protected cleanValue(value: string): string {
return value;
}
protected isValid(value: string): boolean {
return true;
}
}