Skip to content

Commit 9d6e134

Browse files
committed
validation feature component added
1 parent 9292fa5 commit 9d6e134

File tree

3 files changed

+105
-0
lines changed

3 files changed

+105
-0
lines changed
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
export interface AbpValidationError {
2+
name: string;
3+
localizationKey: string;
4+
propertyKey: string;
5+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<ng-container *ngIf="control.invalid && (control.dirty || control.touched)">
2+
<ng-container *ngFor="let validationError of validationErrors">
3+
<span
4+
*ngIf="!!this.control.errors[validationError.name]"
5+
class="error invalid-feedback"
6+
[class.d-block]="!!this.control.errors[validationError.name]"
7+
>
8+
{{ getValidationErrorMessage(validationError) }}
9+
</span>
10+
</ng-container>
11+
</ng-container>
Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
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

Comments
 (0)