I have a custom form control where I implemented the ControlValueAccessor interface, everything is working correctly. However, I want to make a change to the component based on validations, for example, I would like to add a marker to the '*' input label, in case the Validation.required was passed for the case of ReactiveForms or the required attribute was passed for the tag of the component in case of TemplateDrivenForms. Does anyone have any suggestions on how I can do this?
import { Component, EventEmitter, Input, OnChanges, Output, SimpleChanges, forwardRef } from '@angular/core';
import { CommonModule } from '@angular/common';
import { ControlValueAccessor, FormsModule, NG_VALUE_ACCESSOR } from '@angular/forms';
@Component({
selector: 'app-input',
standalone: true,
imports: [CommonModule, FormsModule],
templateUrl: './input.component.html',
styleUrls: ['./input.component.scss'],
providers: [
{
provide: NG_VALUE_ACCESSOR,
useExisting: forwardRef(() => InputComponent),
multi: true
}
]
})
export class InputComponent implements ControlValueAccessor, OnChanges {
protected _value: any = '';
protected _touched: boolean = false;
protected _disabled: boolean = false;
protected _onChange: any = () => {};
protected _onTouch: any = () => {};
@Input() type: string = 'text';
@Input() label: string = '';
writeValue(value: any): void {
this._value = value;
}
registerOnChange(fn: any): void {
this._onChange = fn;
}
registerOnTouched(fn: any): void {
this._onTouch = fn;
}
setDisabledState?(isDisabled: boolean): void {
this._disabled = isDisabled;
}
onChange(value: any): void {
if (!this._disabled) {
this._onChange(value);
}
}
onBlur(): void {
if (!this._touched) {
this._onTouch();
this._touched = true;
}
}
}