|
| 1 | +import { |
| 2 | + Component, |
| 3 | + Input, |
| 4 | + Injector, |
| 5 | + Renderer2, |
| 6 | + ElementRef, |
| 7 | + OnInit |
| 8 | +} from '@angular/core'; |
| 9 | +import { AbstractControl } from '@angular/forms'; |
| 10 | +import { AppComponentBase } from '@shared/app-component-base'; |
| 11 | +import { AbpValidationError } from './abp-validation.api'; |
| 12 | + |
| 13 | +@Component({ |
| 14 | + selector: 'abp-validation-summary', |
| 15 | + templateUrl: './abp-validation.summary.component.html' |
| 16 | +}) |
| 17 | +export class ApbValidationSummaryComponent extends AppComponentBase |
| 18 | + implements OnInit { |
| 19 | + @Input() control: AbstractControl; |
| 20 | + @Input() controlEl: ElementRef; |
| 21 | + @Input() set customValidationErrors(val: AbpValidationError[]) { |
| 22 | + if (val && val.length > 0) { |
| 23 | + let defaults = this.defaultValidationErrors.filter( |
| 24 | + (defaultValidationError) => |
| 25 | + !val.find( |
| 26 | + (customValidationError) => |
| 27 | + customValidationError.name === defaultValidationError.name |
| 28 | + ) |
| 29 | + ); |
| 30 | + this.validationErrors = <AbpValidationError[]>[...defaults, ...val]; |
| 31 | + } |
| 32 | + } |
| 33 | + |
| 34 | + defaultValidationErrors: Partial<AbpValidationError>[] = [ |
| 35 | + { name: 'required', localizationKey: 'ThisFieldIsRequired' }, |
| 36 | + { |
| 37 | + name: 'minlength', |
| 38 | + localizationKey: 'PleaseEnterAtLeastNCharacter', |
| 39 | + propertyKey: 'requiredLength', |
| 40 | + }, |
| 41 | + { |
| 42 | + name: 'maxlength', |
| 43 | + localizationKey: 'PleaseEnterNoMoreThanNCharacter', |
| 44 | + propertyKey: 'requiredLength', |
| 45 | + }, |
| 46 | + { |
| 47 | + name: 'email', |
| 48 | + localizationKey: 'InvalidEmailAddress', |
| 49 | + }, |
| 50 | + { |
| 51 | + name: 'pattern', |
| 52 | + localizationKey: 'InvalidPattern', |
| 53 | + propertyKey: 'requiredPattern', |
| 54 | + }, |
| 55 | + { |
| 56 | + name: 'validateEqual', |
| 57 | + localizationKey: 'PairsDoNotMatch', |
| 58 | + }, |
| 59 | + ]; |
| 60 | + validationErrors = <AbpValidationError[]>this.defaultValidationErrors; |
| 61 | + |
| 62 | + constructor(injector: Injector, public _renderer: Renderer2) { |
| 63 | + super(injector); |
| 64 | + } |
| 65 | + |
| 66 | + ngOnInit() { |
| 67 | + if (this.controlEl) { |
| 68 | + this.control.valueChanges.subscribe(() => { |
| 69 | + if ( |
| 70 | + this.control.valid && |
| 71 | + (this.control.dirty || this.control.touched) |
| 72 | + ) { |
| 73 | + this._renderer.removeClass(this.controlEl, 'is-invalid'); |
| 74 | + } |
| 75 | + }); |
| 76 | + } |
| 77 | + } |
| 78 | + |
| 79 | + getValidationErrorMessage(error: AbpValidationError): string { |
| 80 | + if (this.controlEl) { |
| 81 | + this._renderer.addClass(this.controlEl, 'is-invalid'); |
| 82 | + } |
| 83 | + |
| 84 | + let propertyValue = this.control.errors[error.name][error.propertyKey]; |
| 85 | + return !!propertyValue |
| 86 | + ? this.l(error.localizationKey, propertyValue) |
| 87 | + : this.l(error.localizationKey); |
| 88 | + } |
| 89 | +} |
0 commit comments