4

As you can see I have a father form component with a component child. The {{myFormFather.value}} show me nickName value and name value but dont show age value. {{myFormFather.status}} it dont recognice the component child. Is like my child was a phantom, why?

my-form-father.html

<form [formGroup]="myFormFather" (ngSubmit)="onSubmit()">
    <input formControlName="nickName">
    <input formControlName="name">
    <my-form-child
            [age]="myFormFather">
    </my-form-child>
    <button type="submit"
            [disabled]="myFormFather.invalid">Save
    </button>
</form>

my-form-father.ts

myFormFather = new FormGroup({
    nickName: new FormControl(),
    name: new FormControl()
});

constructor(private fb:FormBuilder) {}

ngOnInit() {this.createForm()}

createForm() {
    this.myFormFather = this.fb.group({
        nickName: ['', [Validators.required],
        name: ['', [Validators.required]
    });
}

my-form-child.html

<div [formGroup]="ageForm">
    <input formControlName="age">
</div>

my-form-child.ts

@Input() ageForm = new FormGroup({
    age: new FormControl()
});

constructor(private fb:FormBuilder) {}

ngOnInit() {this.createForm()}

createForm() {
    this.ageForm = this.fb.group({
        age: ['', [Validators.required]]
    });
}
2
  • Where is [age]="myFormFather" input in your child component? And what are you expecting if you create new FormGroup even twice in child component? Commented Mar 2, 2018 at 6:01
  • 1
    I just wanted to comment, here, that this solution for child form components is severely underrated. Typically with parent/child reactive forms you have the parent dictating the entire form structure, which always seems like it defeats the purpose of having child components to begin with. This allows the child component to manage its own form group, which virtually completely decouples it from the parent, which is fantastic. Commented Aug 2, 2023 at 2:29

1 Answer 1

7

Hi, the solution you have been looking for

Demo Stack Blitz

Problem was: You overwrote the form group being passed from the parent

@Input() ageForm = new FormGroup({
  age: new FormControl()
});

constructor(private fb: FormBuilder) {}

ngOnInit() {
  this.createForm()
}

createForm() {
  // This is overwriting the FormGroup passed from parent!
  this.ageForm = this.fb.group({
    age: ['', [Validators.required]]
  });

  // Instead you had to use
  // this.ageForm.addControl("age", new FormControl('', Validators.required));
}
Sign up to request clarification or add additional context in comments.

4 Comments

Thank you so much! it works! and thank youso much for your stackblitz!
@Hector, It was my pleasure.
Great. But when it's about editing, how can I set child information from father ?
I just wanted to comment, here, that this solution for child form components is severely underrated. Typically with parent/child reactive forms you have the parent dictating the entire form structure, which always seems like it defeats the purpose of having child components to begin with. This allows the child component to manage its own form group, which virtually completely decouples it from the parent, which is fantastic.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.