FormBuilder's updateValue does not exist but still works


#1

Fellow Ionites

I’m have a form created with FormBuilder, and I wish to manually at certain points to update the ‘value’ fields (both displayed and actual values) of its controls.

I’m using updateValue method, but Visual Studio complains that does not have such a method. Compiler also complains the same. Still the form works like a charm.

Am I doing something wrong? or is it a bug in the definitions of FormBuilder and its compoents?
Am I treading on ‘supported’ waters here and fall-through a hole in the future?

part of my myForm.html

<form [ngFormModel]="_myForm">

        <ion-item>
            <ion-label>First Name</ion-label>
            <ion-input #FirstName ngControl="FirstName" type="text"></ion-input>
        </ion-item>
</form>

part of myForm.ts

private _formData={FirstName: "A first name"};

    private _myForm:ControlGroup=this._formBuilder.group({
        FirstName:['',Validators.required]
});

private _refreshControlsWithProfileData(){
            this._myForm.controls['FirstName'].updateValue(this._formData.FirstName);
    }

Thank you in advance


#2

I have not used the updateValue method but in my autocomplete it’s showing _updateValue instead. Do you still get compiler error if you use _updateValue?


#3

_updateValue complains differently. It complains that it should not get any parameters.
And on top it does not work. It does not update my controls.

hmmm… very strange!


#4

I can’t say for certain how safe you are, but I can tell you why it’s happening. The controls map of ControlGroup (or FormGroup in reactive-land) contains AbstractControls. updateValue is defined in the FormControl subclass. So if what you actually have is a FormControl, updateValue() will work. Form groups can contain non-controls as well (for example other nested form groups), and trying to call updateValue() on those will fail.