这个太常用了,一定有一个简单的方法来解决这个问题!这就是这种情况。我有一个表单组:
<ion-item-group formGroupName="alternativeRevenue">
<ion-item-divider color="primary" text-wrap sticky>Alternative revenue </ion-item-divider>
<ion-item>
<ion-label>Branded content</ion-label>
<ion-toggle formControlName="brandedContent"></ion-toggle>
</ion-item>
<ion-item text-wrap *ngIf="businessFoundationsForm.get('alternativeRevenue.brandedContent').value">
<ion-label floating>you want to work with?</ion-label>
<ion-input formControlName="typesOfBrands"></ion-input>
</ion-item>
<ion-item>
<ion-label>Merchandise</ion-label>
<ion-toggle formControlName="merchandise"></ion-toggle>
</ion-item>
<ion-item text-wrap *ngIf="businessFoundationsForm.get('alternativeRevenue.merchandise').value">
<ion-label floating>What types of brands</ion-label>
<ion-input formControlName="typeOfMerchandise"></ion-input>
</ion-item>
<ion-item>
<ion-label>Podcasts</ion-label>
<ion-toggle formControlName="podcasts"></ion-toggle>
</ion-item>
<ion-item text-wrap *ngIf="businessFoundationsForm.get('alternativeRevenue.podcasts').value">
<ion-label floating>Brainstorm topic ideas </ion-label>
<ion-textarea fz-elastic formControlName="podcastIdeas"></ion-textarea>
</ion-item>
<ion-item>
<ion-label>Tours</ion-label>
<ion-toggle formControlName="tours"></ion-toggle>
</ion-item>
<ion-item text-wrap *ngIf="businessFoundationsForm.get('alternativeRevenue.tours').value">
<ion-label floating>Brainstorm tour </ion-label>
<ion-textarea fz-elastic formControlName="tourIdeas"></ion-textarea>
</ion-item>
<ion-item>
<ion-label>Deals</ion-label>
<ion-toggle formControlName="licensingDeals"></ion-toggle>
</ion-item>
<ion-item text-wrap *ngIf="businessFoundationsForm.get('alternativeRevenue.licensingDeals').value">
<ion-label floating>Two ideas for licensing</ion-label>
<ion-textarea fz-elastic formControlName="licensingIdeas"></ion-textarea>
</ion-item>
</ion-item-group>
这是组件中的表单组:
alternativeRevenue: this.fb.group({
brandedContent: [false],
typesOfBrands: [null],
merchandise: [false],
typeOfMerchandise: [null],
podcasts: [false],
podcastIdeas: [null],
tours: [false],
tourIdeas: [null],
licensingDeals: [false],
licensingIdeas: [null]
}, {validator: alternativeRevenueGroupValidator})
我的目标是,如果 select / ion-toggle 之一为真,并且关联的输入字段不为空并且最小长度超过 10,则验证 from 组。如果我将 [Validators.required, Validators.minLength(10)] 添加到所有字段,它将要求在验证表单组之前填写所有字段。我只希望其中一个得到验证,然后整个组都得到验证。我怎样才能做到这一点?
更新:这里是alternativeRevenueGroupValidator。
function alternativeRevenueGroupValidator(c: AbstractControl) {
let brandedContent = c.get('brandedContent');
let typesOfBrands = c.get('typesOfBrands');
let merchandise = c.get('merchandise');
let typeOfMerchandise = c.get('typeOfMerchandise');
let podcasts = c.get('podcasts');
let podcastIdeas = c.get('podcastIdeas');
let tours = c.get('tours');
let tourIdeas = c.get('tourIdeas');
let licensingDeals = c.get('licensingDeals');
let licensingIdeas = c.get('licensingIdeas');
if (brandedContent || merchandise || podcasts|| tours || licensingDeals) {
if (typesOfBrands.value || typeOfMerchandise.value || podcastIdeas.value || tourIdeas.value || licensingIdeas.value) {
return null;
}
}
return {'GroupNoValue': true};
}
如您所见,它非常丑陋。它只能验证输入字段是否有值。我不能重视最小或最大长度或使用这样的库在此处输入链接描述