I use a component with reactive form in a tab. Each time when I switch the tab, the validator of my component will be called once more. This is my component:
import { Component, forwardRef, OnInit } from "@angular/core";
import {
AbstractControl,
FormBuilder,
FormGroup,
NG_VALIDATORS,
NG_VALUE_ACCESSOR,
ValidationErrors,
Validator,
Validators
} from "@angular/forms";
@Component({
selector: "app-my-form",
templateUrl: "./my-form.component.html",
providers: [
{
provide: NG_VALUE_ACCESSOR,
useExisting: forwardRef(() => MyFormComponent),
multi: true
},
{
provide: NG_VALIDATORS,
useExisting: forwardRef(() => MyFormComponent),
multi: true
}
]
})
export class MyFormComponent implements Validator, OnInit {
public form: FormGroup;
constructor(fb: FormBuilder) {
this.form = fb.group({
name: [null, Validators.required]
});
}
// ControlValueAccessor
public writeValue(value: any): void {
if (value) {
this.form.patchValue(value, { emitEvent: false });
}
if (value === null) {
this.form.reset();
}
}
public registerOnChange(fn: any): void {
this.form.valueChanges.subscribe(fn);
}
public registerOnTouched(fn: any): void {
this.onTouched = fn;
}
public setDisabledState?(isDisabled: boolean): void {
isDisabled ? this.form.disable() : this.form.enable();
}
// ControlValueAccessor end
// Validator
validate(control: AbstractControl): ValidationErrors {
console.log("validate");
return null;
}
// Validator end
public onTouched: () => void = () => {};
ngOnInit() {}
}
I also made a stackblitz: https://stackblitz.com/edit/angular-tab-test-hx71jy?file=src%2Fapp%2Fmy-form%2Fmy-form.component.ts
what am I doing wrong?