[英]conditional validation of Angular reactive forms
我是 angular 的新手,正在研究反應形式。
我想根據以下情況添加驗證
[disabled]=".myform.valid"
實現了這一點我試過這個來實現
IsFormValid(){return (!!this.myfm.get('myform').value);}// but this returning always true.
這是我的代碼
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 is being created
(<FormGroup>this.myfm.get('myform')).push(this.addEntried());
}
我知道這是否有點棘手,但仍然沒有得到解決方案。 對此的任何幫助都會對我很有幫助。
我的組件.html 代碼
<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" 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>
嘗試通過檢查響應式表單觸摸屬性來禁用您的保存按鈕。 當用戶對響應式表單控件(鍵入、切換)的操作更改表單時,會觸摸表單。
請注意:您的復選框或輸入必須通過formControl
或formControlName
與您的反應式 forms 連接(如果此示例不適合您,請同時分享 HTML 代碼)
[disabled]="!form.touched"
// or
[disabled]="form.untouched"
這是復制示例https://stackblitz.com/edit/angular-bootstrap-4-starter-4kmswc?file=src/app/app.component.ts
為什么不使用 FormArray 來完成任務? 由於您有一個 formArray 並且在一個 formArray 內,您應該使用兩個函數:
//you can defined your formArray like
formArray=this.fb.array([0,1,2,3,4,5,6,7].map(_=>this.addformFields()))
getGroup(i)
{
return this.formArray.at(i) as FormGroup
}
tasks(i)
{
return this.getGroup(i).get('tasks') as FormArray
}
你的 function addformFields 變得像
addformFields(): FormGroup {
return this.fb.group({
noTask:false,
tasks:this.fb.array([0,1,2,3,4,5,6,7,8,9,10,11].map(_=>this.fb.control(0)))
},{validator:this.someValue()})
}
驗證器就像
someValue(){
return (control:AbstractControl)=>{
const group=control as FormGroup;
let valid=control.get('noTask').value;
(control.get('tasks') as FormArray).controls.forEach(x=>{
valid=valid || +x.value!=0
})
return valid?null:{error:"You shoul indicate one task or that there' not task"}
}
}
.html來控制FormArray
<table class="form-group">
<tr>
<th>Section</th>
<th *ngFor="let i of [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11]">Task {{ i }}</th>
</tr>
<tbody>
<tr
*ngFor="let group of formArray.controls; let i = index"
[formGroup]="getGroup(i)"
>
<td><input type="checkbox" formControlName="noTask" /></td>
<ng-container formArrayName="tasks">
<td *ngFor="let control of tasks(i).controls; let j = index">
<input
[attr.disabled]="getGroup(i).get('noTask').value ? true : null"
[formControlName]="j"
/>
</td>
</ng-container>
</tr>
</tbody>
</table>
看看,如果你定義了 a.css 之類的
tr.ng-invalid.ng-touched input{
border:1px solid red;
}
tr.ng-invalid.ng-touched input:focus{
border-color: red;
border-radius: .25rem;
border-width: 2px;
outline: 0;
box-shadow: 0 0 0 .15rem rgba(255,0,0,.25);
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.