Reputation: 5356
I am trying to use angular material stepper with a single form, each step has many radio input type questions:
<form [formGroup]="formGroup" (ngSubmit)="onSubmit()">
<mat-vertical-stepper [linear]="isLinear">
<!-- Sociaoeconomic attributes -->
<mat-step formGroupName="groupOne" [stepControl]="groupOne" label="Step 1">
<div class="example-selected-value">Education</div>
<mat-radio-group required class="example-radio-group" name="education">
<mat-radio-button class="example-radio-button" *ngFor="let choice of choices" [value]="choice">
{{choice}}
</mat-radio-button>
</mat-radio-group>
<div class="example-selected-value">Literacy</div>
<mat-radio-group required class="example-radio-group" name="literacy">
<mat-radio-button class="example-radio-button" *ngFor="let choice of choices" [value]="choice">
{{choice}}
</mat-radio-button>
</mat-radio-group>
<div>
<button mat-button matStepperNext type="button">Next</button>
</div>
</mat-step>
<!-- Personality variables -->
<mat-step formGroupName="groupTwo" [stepControl]="groupTwo" label="Step 2">
<div class="example-selected-value">Education</div>
<mat-radio-group required class="example-radio-group" name="education">
<mat-radio-button class="example-radio-button" *ngFor="let choice of choices" [value]="choice">
{{choice}}
</mat-radio-button>
</mat-radio-group>
<div class="example-selected-value">Education</div>
<mat-radio-group required class="example-radio-group" name="education">
<mat-radio-button class="example-radio-button" *ngFor="let choice of choices" [value]="choice">
{{choice}}
</mat-radio-button>
</mat-radio-group>
<div>
<button mat-button matStepperPrevious type="button">Previous</button>
<button mat-button matStepperNext type="button">Next</button>
</div>
</mat-step>
<!-- Communication behavior -->
<mat-step formGroupName="groupThree" [stepControl]="groupThree" label="Step 3">
<div class="example-selected-value">Cosmopoliteness</div>
<mat-radio-group required class="example-radio-group" name="cosmopoliteness">
<mat-radio-button class="example-radio-button" *ngFor="let choice of choices" [value]="choice">
{{choice}}
</mat-radio-button>
</mat-radio-group>
<div class="example-selected-value">oleadership</div>
<mat-radio-group required class="example-radio-group" name="oleadership">
<mat-radio-button class="example-radio-button" *ngFor="let choice of choices" [value]="choice">
{{choice}}
</mat-radio-button>
</mat-radio-group>
<div>
<button mat-button matStepperPrevious type="button">Previous</button>
<button mat-button type="submit">Submit</button>
</div>
</mat-step>
</mat-vertical-stepper>
</form>
And in my component:
isLinear = false;
formGroup: FormGroup;
choices : any =["Strongly disagree", "Disagree", "Neutral", "Agree", "Strongly agree"];
constructor(private _formBuilder: FormBuilder) { }
ngOnInit() {
this.formGroup = this._formBuilder.group({
groupOne: this._formBuilder.group({
education: [''],
literacy:['']
}),
groupTwo: this._formBuilder.group({
empathy: [''],
rationslity: ['']
}),
groupThree: this._formBuilder.group({
cosmopliteness: [''],
oleadership:['']
}),
});
}
onSubmit(){
console.log(this.formGroup.value);
}
However onSubmit gives me empty values for all radio buttons, even though I selected. What am I doing wrong?
Upvotes: 1
Views: 1333
Reputation: 11000
You use name="literacy"
in Template drive form, to create a FormControl under this name. In Model driven / Reactive form, you assign field to the created FormControl like formControlName="literacy"
. So try to edit it:
<div class="example-selected-value">Education</div>
<mat-radio-group required class="example-radio-group" formControlName="education">
<mat-radio-button class="example-radio-button" *ngFor="let choice of choices" [value]="choice">
{{choice}}
</mat-radio-button>
</mat-radio-group>
<div class="example-selected-value">Literacy</div>
<mat-radio-group required class="example-radio-group" formControlName="literacy">
<mat-radio-button class="example-radio-button" *ngFor="let choice of choices" [value]="choice">
{{choice}}
</mat-radio-button>
</mat-radio-group>
<div>
Upvotes: 1