diff --git a/projects/igniteui-angular/src/lib/combo/combo.common.ts b/projects/igniteui-angular/src/lib/combo/combo.common.ts index e78a67c49b4..6b8c879a316 100644 --- a/projects/igniteui-angular/src/lib/combo/combo.common.ts +++ b/projects/igniteui-angular/src/lib/combo/combo.common.ts @@ -1300,6 +1300,11 @@ export abstract class IgxComboBaseDirective implements IgxComboBase, AfterViewCh this.manageRequiredAsterisk(); }; + /** @hidden @internal */ + protected externalValidate(): IgxInputState { + return this._valid; + } + private updateValidity() { if (this.ngControl && this.ngControl.invalid) { this.valid = IgxInputState.INVALID; diff --git a/projects/igniteui-angular/src/lib/combo/combo.component.html b/projects/igniteui-angular/src/lib/combo/combo.component.html index 9e795a6f3d3..b97ae325338 100644 --- a/projects/igniteui-angular/src/lib/combo/combo.component.html +++ b/projects/igniteui-angular/src/lib/combo/combo.component.html @@ -9,6 +9,8 @@ { it('should add/remove asterisk when setting validators dynamically', () => { let inputGroupIsRequiredClass = fixture.debugElement.query(By.css('.' + CSS_CLASS_INPUTGROUP_REQUIRED)); let asterisk = window.getComputedStyle(fixture.debugElement.query(By.css('.' + CSS_CLASS_INPUTGROUP_LABEL)).nativeElement, ':after').content; + input = fixture.debugElement.query(By.css(`.${CSS_CLASS_COMBO_INPUTGROUP}`)); expect(asterisk).toBe('"*"'); expect(inputGroupIsRequiredClass).toBeDefined(); + expect(input.nativeElement.getAttribute('aria-required')).toMatch('true'); fixture.componentInstance.reactiveForm.controls.townCombo.clearValidators(); fixture.componentInstance.reactiveForm.controls.townCombo.updateValueAndValidity(); @@ -3446,6 +3448,7 @@ describe('igxCombo', () => { asterisk = window.getComputedStyle(fixture.debugElement.query(By.css('.' + CSS_CLASS_INPUTGROUP_LABEL)).nativeElement, ':after').content; expect(asterisk).toBe('none'); expect(inputGroupIsRequiredClass).toBeNull(); + expect(input.nativeElement.getAttribute('aria-required')).toMatch('false'); fixture.componentInstance.reactiveForm.controls.townCombo.setValidators(Validators.required); fixture.componentInstance.reactiveForm.controls.townCombo.updateValueAndValidity(); @@ -3454,6 +3457,7 @@ describe('igxCombo', () => { asterisk = window.getComputedStyle(fixture.debugElement.query(By.css('.' + CSS_CLASS_INPUTGROUP_LABEL)).nativeElement, ':after').content; expect(asterisk).toBe('"*"'); expect(inputGroupIsRequiredClass).toBeDefined(); + expect(input.nativeElement.getAttribute('aria-required')).toMatch('true'); }); it('Should update validity state when programmatically setting errors on reactive form controls', fakeAsync(() => { diff --git a/projects/igniteui-angular/src/lib/date-picker/date-picker.component.html b/projects/igniteui-angular/src/lib/date-picker/date-picker.component.html index 81159a032bd..29a0c74fbbe 100644 --- a/projects/igniteui-angular/src/lib/date-picker/date-picker.component.html +++ b/projects/igniteui-angular/src/lib/date-picker/date-picker.component.html @@ -15,6 +15,7 @@ } { expect(datePicker).toBeDefined(); expect(inputGroup.isRequired).toBeTruthy(); + expect((datePicker as any).inputDirective.nativeElement.getAttribute('aria-required')).toEqual('true'); }); it('should update inputGroup isRequired correctly', () => { const inputGroup = (datePicker as any).inputGroup; + const inputEl = (datePicker as any).inputDirective.nativeElement; expect(datePicker).toBeDefined(); expect(inputGroup.isRequired).toBeTruthy(); + expect(inputEl.getAttribute('aria-required')).toEqual('true'); (fixture.componentInstance as IgxDatePickerNgModelComponent).isRequired = false; fixture.detectChanges(); expect(inputGroup.isRequired).toBeFalsy(); + expect(inputEl.getAttribute('aria-required')).toEqual(null); }); it('should set validity to initial when the form is reset', fakeAsync(() => { diff --git a/projects/igniteui-angular/src/lib/directives/input/input.directive.ts b/projects/igniteui-angular/src/lib/directives/input/input.directive.ts index daf01c89904..1bf3611ff38 100644 --- a/projects/igniteui-angular/src/lib/directives/input/input.directive.ts +++ b/projects/igniteui-angular/src/lib/directives/input/input.directive.ts @@ -102,6 +102,7 @@ export class IgxInputDirective implements AfterViewInit, OnDestroy { private _valueChanges$: Subscription; private _fileNames: string; private _disabled = false; + private _externalValidate: () => IgxInputState = null; constructor( public inputGroup: IgxInputGroupBase, @@ -195,6 +196,20 @@ export class IgxInputDirective implements AfterViewInit, OnDestroy { this.nativeElement.required = this.inputGroup.isRequired = value; } + /** + * @hidden @internal + * Sets a function to validate the input externally. + * This function should return an `IgxInputState` value. + */ + @Input() + public set externalValidate(fn: () => IgxInputState) { + this._externalValidate = fn; + } + + public get externalValidate(): () => IgxInputState { + return this._externalValidate; + } + /** * Gets whether the igxInput is required. * @@ -367,7 +382,9 @@ export class IgxInputDirective implements AfterViewInit, OnDestroy { * @internal */ protected updateValidityState() { - if (this.ngControl) { + if (this._externalValidate) { + this._valid = this._externalValidate(); + } else if (this.ngControl) { if (!this.disabled && this.isTouchedOrDirty) { if (this.hasValidators) { // Run the validation with empty object to check if required is enabled. diff --git a/projects/igniteui-angular/src/lib/simple-combo/simple-combo.component.html b/projects/igniteui-angular/src/lib/simple-combo/simple-combo.component.html index 21252f137c7..af6d8cbdc57 100644 --- a/projects/igniteui-angular/src/lib/simple-combo/simple-combo.component.html +++ b/projects/igniteui-angular/src/lib/simple-combo/simple-combo.component.html @@ -13,6 +13,7 @@ { expect(combo.valid).toEqual(IgxInputState.INITIAL); expect(combo.comboInput.valid).toEqual(IgxInputState.INITIAL); + expect(combo.comboInput.nativeElement.attributes['aria-required']).toBeDefined(); // empty string combo.open();