19

So I have this form, and it works fine.. but now I would like to extend the json structure some...

https://plnkr.co/edit/aYaYTBRHekHzyS0M7HDM?p=preview

The new structure I want to use looks like this (only address: has changed):

  email: ['', [Validators.required, Validators.email]],
  password: ['', [Validators.required, Validators.minLength(5)]],
  address: this.fb.array([{
    name: '',
    addressLine1: ['', [Validators.required]],
    city: ['', [Validators.required]],
    postalCode: [Validators.required],
  }]),

But I keep getting errors like "ERROR TypeError: control.registerOnChange is not a function". Figured out that this has to do with formControlName missing but I don´t want all data do show..

In the input field I only want addressLine1 to show (not showing name, city, or postalCode at all).

3
  • so when does fileds under address control are used ? thrown error because there is no formControl named address any more instead this is FormArray Commented Jun 1, 2017 at 12:12
  • also i think you misunderstood with the use of FormArray Commented Jun 1, 2017 at 12:14
  • I have updated the plunkr to show where it´s used later on.. Commented Jun 1, 2017 at 12:32

1 Answer 1

48

I would use a form group instead of a formarray for the address, so it would look like this instead:

this.registrationForm = fb.group({
  email: ['', [Validators.required, Validators.email]],
  password: ['', [Validators.required, Validators.minLength(5)]],
  address: this.fb.group({ // make a nested group
    name: '',
    addressLine1: ['', [Validators.required]],
    city: ['', [Validators.required]],
    postalCode: [Validators.required],
  }),
});

Then in your template, remember to mark the formGroupName:

<md-input-container formGroupName="address"> <!-- Mark the nested formgroup name -->
   <input mdInput type="text" placeholder="AddressLine1" name="address" 
         formControlName="addressLine1" fodiGoogleplace 
         (updateAdress)="setAdressOnChange($event)">
</md-input-container>

Your forked PLUNKER

Sign up to request clarification or add additional context in comments.

2 Comments

This is exactly what I ended up doing :)
Awesome! Yeah, FormArray would be a good solution if you wanted to push several address-formgroups inside it. But glad the solution was satisfactory! Have nice day and happy coding! :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.