Angularjs 2/4 Form validation with complex shaper

I am creating a form with a complex FormBuilder.

this.myForm = this._fb.group({
        name: ['', [Validators.required, Validators.minLength(5)]],
        addresses: this._fb.group({
          street: ['pp', Validators.required],
          state: {
            city: ['New York']
          },
          postcode: ['']
        })
});

      

In the nested form, I have a field where the city can be updated. How can I use formControlName for it.

Plunkr

+3


source to share


1 answer


You just do state

as a formGroup, and inside this form control city

:

    this.myForm = this._fb.group({
        name: ['', [Validators.required, Validators.minLength(5)]],
        addresses: this._fb.group({
          street: ['pp', Validators.required],
          state: this._fb.group({
            city: ['New York']
          }),
          postcode: ['']
        })
    });

      

template:



<div formGroupName="state">
  <input formControlName="city"/>
</div>

      

Your forked

Plunker

+3


source







All Articles