I am currently learning Angular and working on creating a reactive form.
Within my HTML table, I have generated controls by looping through the data.
I am looking to add validation based on the following cases:
- Upon page load, the Save button should be disabled by default (which I achieved using
)[disabled]="!myform.valid"
- The Save button should only enable when the user enters a value in any of the text boxes or selects a checkbox in that particular row. Selecting a checkbox and adding a value in a text box simultaneously is not allowed. Either the checkbox should be selected or the user can enter a value in any of the text-boxes.
I have attempted to achieve this with the following code snippet:
IsFormValid(){return (!!this.myfm.get('myform').value);}// but this always returns true.
Here is my code:
myfm:FormGroup;
ngOnInit() {
debugger;
this.myfm = this.fb.group({
myform: this.fb.array([this.addformFileds()])
});
}
addformFileds(): FormGroup {
return this.fb.group({
NoTask: ['', Validators.required],// only check box is required either checkbox should click
or enter value in any text-boxes
task1: ['', null],
task2: ['', null],
task3: ['', null],
task4: ['', null],
task5: ['', null],
task6: ['', null],
task7: ['', null],
task8: ['', null],
task9: ['', null],
task10: ['', null],
task11: ['', null],
task12: ['', null]
});
}
//adding more rows on click of Add More Rows button
addEntried():void{
this.idAddNewRow=true; //indicator that new rows are being created
(<FormGroup>this.myfm.get('myform')).push(this.addEntried());
}
I know it's a bit tricky, still haven't found a solution. Any help on this matter would be greatly appreciated.
My component.html code:
<form #frmImp='NgForm' [formGroup]="myfm"]>
<div class="modal-content">
<div class="modal-header" style="align-items: center">
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
</div>
<div class="modal-body">
<!--<table class="table-bordered table-responsive" style="overflow-x: true;"> to disable the scroll bar if screen resolution is less than 1920x1080-->
<table class="table-bordered " >
<thead>
<tr style="border-bottom-style: solid"><td id="" class="" colspan="15" style="text-align: center"><b>Imp Task Details</b></td>
</tr>
<tr>
<th style="text-align: center;"> </th>
<th style="text-align: center;"> </th>
<th style="text-align: center;">No Task</th>
<th style="text-align: center;">Task 1</th>
<th style="text-align: center;">Task 2</th>
<th style="text-align: center;">Task 3</th>
<th style="text-align: center;">Task 4</th>
<th style="text-align: center;">Task 5</th>
<th style="text-align: center;">Task 6</th>
<th style="text-align: center;">Task 7</th>
<th style="text-align: center;">Task 8</th>
<th style="text-align: center;">Task 9</th>
<th style="text-align: center;">Task 10</th>
<th style="text-align: center;">Task 11</th>
<th style="text-align: center;">Task 12</th>
</tr>
</thead>
<tbody formArrayName="myform" **ngFor="let frm of myfm.get('myform')['controls']; let i=index">
<tr [[formGroupName]="i"]>
<td></td>
<td></td>
<td><input [id]="'txt'+i" type="checkbox" formControlName="NoTask" style="margin-left: 30px;"/></td>
<td ><input [id]="'txt'+i" type="tex" class="textboxFormat" formControlName="task1" placeholder="0"></td>
<td ><input [id]="'txt'+i" type="tex" class="textboxFormat" formControlName="task2" ></td>
<td ><input [id]="'txt'+i" type="tex" class="textboxFormat" formControlName="task3" ></td>
<td ><input [id]="'txt'+i" type="tex" class="textboxFormat" formControlName="task4" ></td>
<td ><input [id]="'txt'+i" type="tex" class="textboxFormat" formControlName="task5" ></td>
<td ><input [id]="'txt'+i" type="tex" class="textboxFormat" formControlName="task6" ></td>
<td ><input [id]="'txt'+i" type="tex" class="textboxFormat" formControlName="task7" ></td>
<td ><input [id]="'txt'+i" type="tex" class="textboxFormat" formControlName="task8" ></td>
<td ><input [id]="'txt'+i" type="tex" class="textboxFormat" formControlName="task9" ></td>
<td ><input [id]="'txt'+i" type="tex" class="textboxFormat" formControlName="task10" ></td>
<td ><input [id]="'txt'+i" type="tex" class="textboxFormat" formControlName="task11" ></td>
<td ><input [id]="'txt'+i" type="tex" class="textboxFormat" formControlName="task12" ></td>
</tr>
</tbody>
</table>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
<button type="submit" class="btn btn-primary" *ngIf="isAvailable" (click)="addformFileds()">Add More Rows </button>
<button type="submit" class="btn btn-primary" (click)=SaveImpDetails(frmImp)>Save </button>
</div>
</div>
</form>
**component.ts** file code
SaveImpDetails(){
this.baseSrvc.post(ApiResource.SaveImpDetails,
JSON.stringify(body)).subscribe((result=>{
if(result){
this.alertService.showMessage("Success!", "Details has been
recorded
successfuly.")
}if(result.isError){
this.alert.showMessage("Error!! while saving details");
}
}));
}