How to get a link to FormControl from a component that implements ControlValueAccessor?

I created an Angular component derived from this blog post . I have this in a reactive form, and I would like to get errors in the form control, the component itself will have a stylized error message that will be displayed when the control has an error. However, when I try to introduce the NgControl class into the component, I get circular reference problems, so how can I access the errors in the control?

Here is the current code, it is not complete yet, but it should give a basic idea of ​​what I'm trying to execute:

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>
+6
source share
1 answer

- . , , .

formGroup , formControlName. .

//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 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 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>
+1

Source: https://habr.com/ru/post/1016474/


All Articles