Reputation: 930
I am trying to validate radio button group that are dynamically created from a object of arrays and i want it to be validated in the form i have posted a questions seeking answer and i was not clarified and did some research again and found this article followed its trail and found formArrayName but am unable to achieve the validation
<form[formGroup]="form">
<ion-select formControlName="type">
<ion-option value="1">Full Time</ion-option>
<ion-option value="2">Part Time</ion-option>
</ion-select>
<div formArrayName="employee_details">
<ion-row class="rows" *ngFor="let employee of employee; let rowIndex = index">
<ion-col text-center="text-center" col-6="col-6">
[ {{employee.id}} ]
<br> {{employee.name}}
</ion-col>
<ion-col class="rows last" col-6="col-6">
<ion-list class="row" no-lines="no-lines" radio-group="radio-group">
<ion-item col-4="col-4">
<ion-radio class="radio " mode="md" value="Paid"></ion-radio>
</ion-item>
<ion-item col-4="col-4">
<ion-radio class="radio " mode="md" value="Unpaid"></ion-radio>
</ion-item>
</ion-list>
</ion-col>
</ion-row>
</div>
</form>
JS
import { FormBuilder, FormGroup, FormControl,FormArray, Validators } from '@angular/forms ';
class type {
public name: string;
public id: string;
}
...
class employee {
public name: string;
public id: string;
}
...
employee: employee = [{
id: 1,
name: "emp1"
}, {
id: 2,
name: "emp2"
}, {
id: 3,
name: "emp3"
}];
form: FormGroup;
this.form = new FormGroup({
'type': new FormControl(null, [Validators.required]),
'employee_details': new FormArray([])
});
Object.keys(this.employee).forEach(function(key, index) {
console.log(key);
// problem is here
const control = new FormControl(null, Validators.required);
( < FormArray > this.form.get('employee_details')).push(control);
});
Upvotes: 0
Views: 918
Reputation: 235
This is not formArray, but it will definitely helps you..
.html
<form [formGroup]="form">
<ion-row class="rows" *ngFor="let detail of details; let i = index">
<ion-col text-center="text-center" col-6="col-6">
{{your_data}}
</ion-col>
<ion-col class="rows last" col-6="col-6">
<ion-list class="row" no-lines="no-lines" radio-group="radio-group" formControlName="action{{i}}">
<ion-item col-4>
<ion-radio class="radio true" mode="md" value="true"></ion-radio>
</ion-item>
<ion-item col-4>
<ion-radio class="radio false" mode="md" value="false"></ion-radio>
</ion-item>
<ion-item col-4>
<ion-radio class="radio " mode="md" value="not sure"></ion-radio>
</ion-item>
</ion-list>
</ion-col>
</ion-row>
<button ion-button (click)="submit()" [disabled]="!form.valid">Submit</button>
</form>
.ts Add this code where you get data.
let i;
for(i in this.details){
this.form.setControl('action'+i, new FormControl('', Validators.required));
}
variable 'i' just used to produce 0,1,2,3 to add with control name. you can use your way to do this but loop is required.
Upvotes: 1
Reputation: 39482
Instead of doing what you're doing right now, you can simply map
through the array and then for each element, just create a FormControl
Try this:
this.form = new FormGroup({
type: new FormControl(null, [Validators.required]),
employee_details: new FormArray([this.employee.map(emp => new FormControl(null, Validators.required))])
});
Upvotes: 1