SUPPORT-8971: add validation by idm
This commit is contained in:
parent
b45e478148
commit
26ae0cdfce
4 changed files with 39 additions and 551 deletions
|
|
@ -1,8 +1,8 @@
|
|||
import {Behavior, TextField} from "@webbpm/base-package";
|
||||
import {SimpleValidator, TextField} from "@webbpm/base-package";
|
||||
import {Observable} from "rxjs";
|
||||
import {IdmService} from "../IdmService";
|
||||
|
||||
export abstract class IdmTextFieldValidation extends Behavior {
|
||||
export abstract class AbstractIdmValidator extends SimpleValidator {
|
||||
protected abstract ERROR_MESSAGE: string;
|
||||
protected abstract checkValueExists(value: string): Observable<boolean>;
|
||||
|
||||
|
|
@ -18,35 +18,35 @@ export abstract class IdmTextFieldValidation extends Behavior {
|
|||
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;
|
||||
async isValid(): Promise<boolean> {
|
||||
const inputValue: string = this.textInput.value;
|
||||
if (!inputValue) return Promise.resolve(false);
|
||||
|
||||
const cleanedValue = this.cleanValue(inputValue);
|
||||
|
||||
if (!this.isValid(cleanedValue)) return;
|
||||
if (!this.isPatternValid(cleanedValue)) return Promise.resolve(false);
|
||||
|
||||
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);
|
||||
return this.checkValueExists(cleanedValue).toPromise().then(
|
||||
(exists) => {
|
||||
this.pushError(!exists, this.ERROR_MESSAGE);
|
||||
return !exists;
|
||||
},
|
||||
(err) => {
|
||||
const fieldName = this.textField.getObjectName();
|
||||
console.error(`Ошибка проверки ${fieldName}: ${err.message}`);
|
||||
return false;
|
||||
}
|
||||
},
|
||||
error: (err) => {
|
||||
console.error(`Ошибка проверки: ${err}`);
|
||||
}
|
||||
});
|
||||
);
|
||||
}
|
||||
|
||||
private async onBlurHandler(event: FocusEvent): Promise<void> {
|
||||
await this.isValid();
|
||||
}
|
||||
|
||||
protected cleanValue(value: string): string {
|
||||
return value;
|
||||
}
|
||||
|
||||
protected isValid(value: string): boolean {
|
||||
protected isPatternValid(value: string): boolean {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
|
@ -1,14 +1,14 @@
|
|||
import {IdmTextFieldValidation} from "./IdmTextFieldValidation";
|
||||
import {Observable} from "rxjs";
|
||||
import {AbstractIdmValidator} from "./AbstractIdmValidator";
|
||||
|
||||
export class IdmLoginTextFieldValidation extends IdmTextFieldValidation {
|
||||
export class IdmLoginValidator extends AbstractIdmValidator {
|
||||
protected ERROR_MESSAGE = "Пользователь с указанным логином уже существует";
|
||||
|
||||
protected checkValueExists(value: string): Observable<boolean> {
|
||||
return this.idmService.checkLoginExists(value);
|
||||
}
|
||||
|
||||
protected isValid(value: string): boolean {
|
||||
protected isPatternValid(value: string): boolean {
|
||||
let pattern = this.context.pattern;
|
||||
return pattern ? new RegExp(pattern).test(value) : true;
|
||||
}
|
||||
|
|
@ -1,7 +1,7 @@
|
|||
import {IdmTextFieldValidation} from "./IdmTextFieldValidation";
|
||||
import {Observable} from "rxjs";
|
||||
import {AbstractIdmValidator} from "./AbstractIdmValidator";
|
||||
|
||||
export class IdmSnilsTextFieldValidation extends IdmTextFieldValidation {
|
||||
export class IdmSnilsValidator extends AbstractIdmValidator {
|
||||
protected ERROR_MESSAGE = "Пользователь с указанным СНИЛС уже существует";
|
||||
|
||||
protected checkValueExists(value: string): Observable<boolean> {
|
||||
Loading…
Add table
Add a link
Reference in a new issue