Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I get reference to the FormControl from a component that implements ControlValueAccessor?

Tags:

angular

I created an Angular component derived from this blog post. I have it in a reactive form and I would like to get the errors on the form control, the component itself will have a stylized error message that it will render when the control has an error on it. However when I try to inject the NgControl class into the component I am getting circular reference issues, so how would I access the errors on the control?

Here is the current code, it's not complete yet but it should give the basic idea of what I am trying to accomplish:

import { Component, Output, EventEmitter, Input, forwardRef } from '@angular/core';
import {
    NgControl,
    NG_VALUE_ACCESSOR,
    ControlValueAccessor,
    Validator,
    AbstractControl,
    FormControl,
    NG_VALIDATORS
} from '@angular/forms';

@Component({
    selector: 'form-field-input',
    templateUrl: './form-field-input.component.html',
    styleUrls: ['./form-field-input.component.less'],
    providers: [{
        provide: NG_VALUE_ACCESSOR,
        useExisting: forwardRef(() => FormFieldInputComponent),
        multi: true
    }]
})
export class FormFieldInputComponent implements ControlValueAccessor {

    private propagateChange = (_: any) => { };
    private propagateTouch = (_: any) => { };

    @Input('label') label: string;
    @Input('type') type: string;
    @Input('id') id: string;
    @Input('formControlName') formControlName: string;
    @Input('error') error: string;
    @Input('classes') classes: any;

    private value: string;
    private data: any;

    constructor() {
        debugger;
    }

    private onChange(event) {
        this.data = event.target.value;
        this.propagateChange(this.data);
        this.propagateTouch(this.data);
    }

    writeValue(obj: any): void {
        this.data = obj;
    }
    registerOnChange(fn: any): void {
        this.propagateChange = fn;
    }
    registerOnTouched(fn: any): void {
        this.propagateTouch = fn;
    }
}

template file:

<div class="form-field-input-component">
    <input id="{{id}}"
           type="{{type}}"
           class="form-field-input"
           [value]="data"
           (change)="onChange($event)"
           (keyup)="onChange($event)" />
    <span class="context-icon fa fa-lock"></span>
    <span class="info-icon fa fa-info-circle"></span>
    <!-- I will have an NGIF here to check for errors before rendering the error -->
    <div class="form-error">
        {{ error }}
    </div>
</div>
like image 262
tt9 Avatar asked Apr 11 '17 16:04

tt9


2 Answers

I was hoping to do this through some sort of dependency injection or declarative style. Since I couldn't find anything using those methods I will share how I fixed this for my case.

I just added the formGroup as an input parameter to the components, along with the formControlName begin passed I am able to get reference to the control.

Here is the end result of my component

//Typescript code file for component
/// ... necessary imports
@Component({
    selector: 'form-field-input',
    templateUrl: './form-field-input.component.html',
    styleUrls: ['./form-field-input.component.less'],
    providers: [{
        provide: NG_VALUE_ACCESSOR,
        useExisting: forwardRef(() => FormFieldInputComponent),
        multi: true
    }]
})
export class FormFieldInputComponent implements ControlValueAccessor {

    private propagateChange = (_: any) => { };
    private propagateTouch = (_: any) => { };

    @Input('label') label: string;
    @Input('type') type: string;
    @Input('id') id: string;
    @Input('contextIconName') contextIconName: string;

    //Here I take in both the parent form and the form control name
    //in ngOnInit I throw if there is no parent form passed
    @Input('formControlName') formControlName: string;
    @Input('parentForm') parentForm: FormGroup;

    @Input('classes') classes: any;
    @Input('errorDefs') errorDefs: any;

    private error: string;
    private value: string;
    private data: any;
    private control: AbstractControl;

    constructor() {}

    ngOnInit() {
        if (!this.parentForm) {
            throw "Form Field input component must be a part of a form group"
        }

        //It ain't pretty but here we get access to the control and all of it's errors
        this.control = this.parentForm.get(this.formControlName);
        if (!this.control) {
            throw "Form Field input component must be a part of a form group"
        }
    }

    private setError() {
        if (this.errorDefs && this.control.errors) {
            var errorKeys = Object.keys(this.control.errors).filter(x => !!x);
            if (errorKeys) {
                var errorKey = errorKeys[0];
                var error = this.errorDefs[errorKey] || null;
                this.error = error;
                return;
            }
        }
        this.error = null;
    }

    //Now on our on change event we can propagate the events 
    //To the registered handlers, which should set the form field errors
    //and at the end we can check the reference to the control for those errors
    //so that we can display the appropriate messages
    private onChange(event) {
        this.data = event ? event.target.value : this.data;
        this.propagateChange(this.data);
        this.propagateTouch(this.data);
        this.setError();
    }

    writeValue(obj: any): void {
        this.data = obj;
    }
    registerOnChange(fn: any): void {
        this.propagateChange = fn;
    }
    registerOnTouched(fn: any): void {
        this.propagateTouch = fn;
    }
}

//HTML template file
<div class="form-field-input-component">
    <input id="{{id}}"
           type="{{type}}"
           class="form-field-input {{class}}"
           [value]="data"
           (change)="onChange($event)"
           (keyup)="onChange($event)"
           (blur)="onChange($event)" />
    <span class="context-icon fa {{contextIconName || 'fa-cog'}}"></span>
    <span class="info-icon fa fa-info-circle" *ngIf="error"></span>
    <div class="form-field-error" *ngIf="error">
        {{ error }}
    </div>
</div>

//EXAMPLE USAGE:
<form novalidate [formGroup]="myFormGroup">
    <form-field-input
                      formControlName="firstName"
                      [parentForm]="myFormGroup"

                      <!-- example: When the Validators.required sets it's error message we can map that to a user friendly error -->
                      [errorDefs]="{
                           'required': 'this field is required' 
                      }"
                      <!-- Other inputs and stuff-->
                      >
    </form-field-input>
</form>
like image 83
tt9 Avatar answered Oct 06 '22 09:10

tt9


You can actually just inject ngControl into the custom input element like this,

(ts file)

import {NgControl} from "@angular/forms";

...

constructor(public ngControl: NgControl) {}

(template)

<label>Custom Input</label>
<input [formControl]="ngControl.control"/>

then in the parent component call it like this,

<custom-input
  [formControl]="youControlHere"
  ngDefualtControl> <!-- This is the part that gives the parent control -->
</custom-input>

If you have this set up it should work like a normal input with formControl! Hope this helps.

like image 39
Aaron Turkel Avatar answered Oct 06 '22 09:10

Aaron Turkel