gpt4 book ai didi

javascript - 多个输入字段之间的表单验证

转载 作者:太空狗 更新时间:2023-10-29 17:56:50 24 4
gpt4 key购买 nike

所以我有一个包含多个输入表单字段和嵌套表单组的表单。在嵌套的表单组中,验证应该是这样的,如果三个输入中的任何一个被填充,那么表单应该是有效的。此人可以填写所有内容,也可以只填写一个,并且表格应该是有效的。我的模板代码如下:

 <h3 class="form-title">{{title}}</h3>
<form [formGroup]="formX" novalidate>
<div formGroupName="brandIdentifier">
<mat-form-field class="full-width">
<mat-icon matSuffix color="primary" *ngIf="brandName.valid && brandName.touched">done</mat-icon>
<mat-icon matSuffix color="primary" *ngIf="brandName.invalid && brandName.touched">close</mat-icon>
<input matInput type="text" placeholder="Brand Name" formControlName="brandName" />
</mat-form-field>
<mat-form-field class="full-width">
<mat-icon matSuffix color="primary" *ngIf="brandId.valid && brandId.touched">done</mat-icon>
<mat-icon matSuffix color="primary" *ngIf="brandId.invalid && brandId.touched">close</mat-icon>
<input matInput type="text" placeholder="Brand Id" formControlName="brandId" />
</mat-form-field>
<mat-form-field class="full-width">
<mat-icon matSuffix color="primary" *ngIf="contentId.valid && contentId.touched">done</mat-icon>
<mat-icon matSuffix color="primary" *ngIf="contentId.invalid && contentId.touched">close</mat-icon>
<input matInput type="text" placeholder="Content Id" formControlName="contentId" />
</mat-form-field>
</div>
<mat-form-field class="full-width">
<mat-icon matSuffix color="primary" *ngIf="startTime.valid && startTime.touched">done</mat-icon>
<mat-icon matSuffix color="primary" *ngIf="startTime.invalid && startTime.touched">close</mat-icon>
<input matInput type="text" placeholder="Start Time" formControlName="startTime" />
</mat-form-field>
<mat-form-field class="full-width">
<mat-icon matSuffix color="primary" *ngIf="endTime.valid && endTime.touched">done</mat-icon>
<mat-icon matSuffix color="primary" *ngIf="endTime.invalid && endTime.touched">close</mat-icon>
<input matInput type="text" placeholder="End Time" formControlName="endTime" />
</mat-form-field>

<button mat-raised-button color="primary" (click)="startAnalysis()" [ngClass]="{'form-valid':formX.valid, 'form-invalid':formX.invalid}">Submit</button>

<pre>{{formX.value | json}}</pre>
</form>

我该怎么做?使用 Validator 类是给定的,但我无法将其设为可选。

最佳答案

您可以为此使用自定义验证器,为嵌套组应用自定义验证器,我们检查三个字段中至少有一个的值不是空字符串,所以这里有一个示例:

构建形式:

this.myForm = this.fb.group({
nestedGroup: this.fb.group({
first: [''],
second: [''],
third: ['']
// apply custom validator for the nested group only
}, {validator: this.myCustomValidator})
});

自定义验证器:

 myCustomValidator(group: FormGroup) {
// if true, all fields are empty
let bool = Object.keys(group.value).every(x => group.value[x] === '')
if(bool) {
// return validation error
return { allEmpty: true}
}
// valid
return null;
}

然后在您的表单中,您可以通过以下方式显示验证消息:

<small *ngIf="myForm.hasError('allEmpty','nestedGroup')">
Please fill at least one field
</small>

最后一个 DEMO :)

关于javascript - 多个输入字段之间的表单验证,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47326920/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com