[ADF-2925] Required and invalid validators added (#3277)

* test added

* tests clean

* test added

* tslint clean

* test name changed

* update tests
This commit is contained in:
bbcodrin 2018-05-15 15:21:38 +03:00 committed by Maurizio Vitale
parent db7d0b7c08
commit fd924c439a
3 changed files with 33 additions and 4 deletions

View File

@ -18,9 +18,12 @@
matInput [formControl]="to" [errorStateMatcher]="matcher" matInput [formControl]="to" [errorStateMatcher]="matcher"
placeholder="{{ 'SEARCH.FILTER.RANGE.TO' | translate }}" placeholder="{{ 'SEARCH.FILTER.RANGE.TO' | translate }}"
autocomplete="off"> autocomplete="off">
<mat-error *ngIf="to.invalid"> <mat-error *ngIf="to.hasError('pattern')">
{{ 'SEARCH.FILTER.VALIDATION.INVALID-FORMAT' | translate }} {{ 'SEARCH.FILTER.VALIDATION.INVALID-FORMAT' | translate }}
</mat-error> </mat-error>
<mat-error *ngIf="to.hasError('required')">
{{ 'SEARCH.FILTER.VALIDATION.REQUIRED-VALUE' | translate }}
</mat-error>
</mat-form-field> </mat-form-field>

View File

@ -136,4 +136,28 @@ describe('SearchNumberRangeComponent', () => {
expect(component.formValidator).toBeTruthy(); expect(component.formValidator).toBeTruthy();
}); });
it('should throw pattern error if "from" value is formed by letters', () => {
component.ngOnInit();
component.from = new FormControl('abc', component.validators);
expect(component.from.hasError('pattern')).toBe(true);
});
it('should not throw pattern error if "from" value is formed by digits', () => {
component.ngOnInit();
component.from = new FormControl(123, component.validators);
expect(component.from.hasError('pattern')).toBe(false);
});
it('should throw required error if "from" value is empty', () => {
component.ngOnInit();
component.from = new FormControl('', component.validators);
expect(component.from.hasError('required')).toBe(true);
});
it('should not throw required error if "from" value is not empty', () => {
component.ngOnInit();
component.from = new FormControl(123, component.validators);
expect(component.from.hasError('required')).toBe(false);
});
}); });

View File

@ -44,6 +44,8 @@ export class SearchNumberRangeComponent implements SearchWidget, OnInit {
field: string; field: string;
format = '[{FROM} TO {TO}]'; format = '[{FROM} TO {TO}]';
validators: Validators;
ngOnInit(): void { ngOnInit(): void {
if (this.settings) { if (this.settings) {
@ -51,13 +53,13 @@ export class SearchNumberRangeComponent implements SearchWidget, OnInit {
this.format = this.settings.format || '[{FROM} TO {TO}]'; this.format = this.settings.format || '[{FROM} TO {TO}]';
} }
const validators = Validators.compose([ this.validators = Validators.compose([
Validators.required, Validators.required,
Validators.pattern(/^-?(0|[1-9]\d*)?$/) Validators.pattern(/^-?(0|[1-9]\d*)?$/)
]); ]);
this.from = new FormControl('', validators); this.from = new FormControl('', this.validators);
this.to = new FormControl('', validators); this.to = new FormControl('', this.validators);
this.form = new FormGroup({ this.form = new FormGroup({
from: this.from, from: this.from,