Skip to content

Commit 9cc10e7

Browse files
IvanKitanov17Ivan Kitanovhanastasov
authored
Adding markAsTouched to fix form validation (#16212)
* fix(input): Adding markAsTouched to fix form validation * chore(input): Remove unnecessary fit --------- Co-authored-by: Ivan Kitanov <[email protected]> Co-authored-by: Hristo <[email protected]>
1 parent b7906a5 commit 9cc10e7

File tree

2 files changed

+24
-0
lines changed

2 files changed

+24
-0
lines changed

projects/igniteui-angular/src/lib/directives/input/input.directive.spec.ts

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -887,6 +887,27 @@ describe('IgxInput', () => {
887887

888888
expect(igxInput.valid).toBe(IgxInputState.INITIAL);
889889
}));
890+
891+
it('should mark the reactive form control as touched when igxInput loses focus', fakeAsync(() => {
892+
const fixture = TestBed.createComponent(ReactiveFormComponent);
893+
fixture.detectChanges();
894+
895+
const component = fixture.componentInstance;
896+
const formControl = component.form.get('str');
897+
const inputDebug = fixture.debugElement.query(By.css('input[formControlName="str"]'));
898+
const input = inputDebug.nativeElement;
899+
900+
expect(formControl.touched).toBe(false);
901+
902+
input.dispatchEvent(new Event('focus'));
903+
fixture.detectChanges();
904+
905+
input.dispatchEvent(new Event('blur'));
906+
tick();
907+
fixture.detectChanges();
908+
909+
expect(formControl.touched).toBe(true);
910+
}));
890911
});
891912

892913
@Component({

projects/igniteui-angular/src/lib/directives/input/input.directive.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -227,6 +227,9 @@ export class IgxInputDirective implements AfterViewInit, OnDestroy {
227227
@HostListener('blur')
228228
public onBlur() {
229229
this.inputGroup.isFocused = false;
230+
if (this.ngControl?.control) {
231+
this.ngControl.control.markAsTouched();
232+
}
230233
this.updateValidityState();
231234
}
232235
/** @hidden @internal */

0 commit comments

Comments
 (0)