Angular reset to pristine from inside ControlValueAccessor implementation

Discussão em 'Angular' iniciado por malloc4k, Agosto 21, 2025.

  1. malloc4k

    malloc4k Guest

    I have a ControlValueAccessor implementation for custom form control:

    @Component({
    ...
    providers: [
    {
    provide: NG_VALUE_ACCESSOR,
    useExisting: forwardRef(() => MyControlComponent),
    multi: true,
    },
    {
    provide: NG_VALIDATORS,
    useClass: MyValidator,
    multi: true,
    }
    ],
    })
    export class MyControlComponent implements ControlValueAccessor {

    public registerOnChange(fn: any): void {
    this.accessorOnChange = fn;
    }

    public someCustomHandler() {

    // triggers MyValidator but also marks form control as dirty
    this.accessorOnChange(value);

    }

    }


    Inside someCustomHandler() I want to trigger the MyValidator privided via NG_VALIDATORS and to do that I use the callback provided in registerOnChange(). It correctly updates the valid state of the form control but also marks it as dirty.

    Is there a way to avoid marking form control as dirty in this case, e.g. by marking it as pristine from inside ControlValueAccessor implementation.

    Continue reading...

Compartilhe esta Página