I am trying to create a directive for case only. I created the following code but I have a problem trying to make formGroup.value. The directive changes the value visually (by using _Renderer2), but the formControl still has the original value and when obtaining the data from the form with formGroup.value, the values are in lowercase. Is it possible to fix that? Thank you
<form [formGroup]="formGroup">
<mat-form-field>
<input placeholder="Street" formControlName="streetName" matInput uppercase>
</mat-form-field>
</form>
import { Directive, ElementRef, Optional, Renderer2, Self } from '@angular/core';
import { ControlValueAccessor, NgControl } from '@angular/forms';
@Directive({
selector: "textarea[uppercase], input[uppercase]",
host: {
'(input)': 'writeValue($event.target.value)',
'(blur)': 'onTouched()',
}
})
export class UppercaseDirective implements ControlValueAccessor {
onChange = (_: any) => {
console.log("onChange", _)
};
onTouched = () => {
console.log("onTouched")
};
constructor(private _renderer: Renderer2, private _elementRef: ElementRef, @Optional() @Self() public ngControl: NgControl) {
ngControl.valueAccessor = this;
}
writeValue(value: any): void {
this._renderer.setProperty(this._elementRef.nativeElement, 'value', this.transformValue(value));
}
registerOnChange(fn: (_: any) => {}): void {
this.onChange = fn;
}
registerOnTouched(fn: () => {}): void {
this.onTouched = fn;
}
setDisabledState(isDisabled: boolean): void {
this._renderer.setProperty(this._elementRef.nativeElement, 'disabled', isDisabled);
}
private transformValue(value: string): string {
return typeof value === 'string'
? value?.toUpperCase()
: value;
}
}
You're mixing the concepts of custom form control and directive. I don't know if there are another aproach, but, if you want to make a directive that transform one input, you can get it creating a HTMLEvent. Some like
NOTE: Really I don't see the directive as a good aproach to the problem on upperCase an input. For me is better simply use css
Then, when we want to check the value transform the value use toUpperCase.