Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Angular2: how to access a formControl value on a nested formBuilder form

How can I get the value of this.resFormGroup.patientName.lastname.value without using [ngModel]?

What are the purpose of name="resForm", [formGroup]="resFormGroup" and #resNgForm="ngForm" in the HTML <form> tag?

Do I need the template reference variable #resNgForm since I have the [formGroup]="resFormGroup"?

This is my component:

// FormBuilder
this.resFormGroup = this.formBuilder.group({
  patientName: this.formBuilder.group({
    firstname: ['', Validators.required],
    lastname: ['', Validators.required]
  }),
  address: this.formBuilder.group({
    street: [],
    zip: [],
    city: []
  })
});

This is my template:

<form name="resForm" [formGroup]="resFormGroup" (ngSubmit)="onSubmit()" #resNgForm="ngForm">
  <fieldset formGroupName="patientName">
      <legend>Name</legend>
  <label>Firstname:</label>
  <input type="text" formControlName="firstname" [(ngModel)]="myFirstName">

  <label>Lastname:</label>
  <input type="text" ***formControlName="lastname"***>
  </fieldset>

  <fieldset formGroupName="address">
      <legend>Address</legend>
      <label>Street:</label>
      <input type="text" formControlName="street">

      <label>Zip:</label>
      <input type="text" formControlName="zip">

      <label>City:</label>
      <input type="text" formControlName="city">
  </fieldset>

  <button type="submit" class="btn btn-primary" [disabled]="!resNgForm.valid">Submit</button>

My submit button:

 onSubmit() { 
   console.log("Submit button pressed [ngModel: " + this.myFirstName + "]    [Form: " + this.resFormGroup.controls["patientName"].dirty + "]"); 
 }

With [ngModel], I can get the first name; I can also access the dirty attribute of the formGroup's patientName.

like image 816
Autorun Avatar asked Sep 29 '16 01:09

Autorun


People also ask

How to use formcontrolname in Angular 2?

FormControlName: It is used with FormGroup with a <form> tag. FormControlName syncs a FormControl in an existing FormGroup to a form control by name. Now let us start how to use FormControl in our angular application. For every form control such as text, checkbox, radio button we need to create an instance of FormControl in our class.

What is formbuilder in angular?

Angular’s FormBuilder helps you streamline the process of building advanced forms while avoiding repetition. Put simply, FormBuilder provides syntactic sugar that eases the burden of creating instances of FormControl, FormGroup, or FormArray and reduces the amount of boilerplate required to build complex forms.

How to get the value of formcontrol in HTML?

When the form will be submitted, we can access form values as following. To validate a particular form control, we need to get value of that form control. For example, to validate name FormControl, we need to create a method in our class as given below. Now use it in HTML. We can also access FormControl value using FormGroupDirective.

How do I access the formcontrol in a nested formgroup?

There are a couple of very simple ways to access the FormControl in a nested FormGroup. From the AbstractControl usage notes: For example, to get a name control nested within a person sub-group:


1 Answers

Based on this tutorial about dynamic form, they used a very simple way to show the values of the whole form without [ngModel].

How to get the formBuilder form values? The answer is 'this.myForm.value'.

Example code

For example:

onSubmit(model: ResCrud) {  
    console.log("Nested Control: " + JSON.stringify(this.resFormGroup.value));
}

How it looks in the web console

angular formbuilder nested value example

like image 89
Autorun Avatar answered Oct 25 '22 07:10

Autorun