이것은 매우 일반적으로 사용되므로이를 접근 할 수있는 간단한 방법이 있어야합니다! 여기 상황이 있습니다. 선택/이온 토글 중 하나에 해당하는 경우 및 그룹의 컨트롤 중 하나에 값이있는 경우 유효성 검사를 통과하기 위해 각도 2 형식으로 간단한 교차 필드 유효성 검사를 수행하는 방법?
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})
가 내 목표는, 관련 입력
을 제출 : 여기<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>
그리고 구성 요소의 양식 그룹이다 : 나는 양식 그룹이 비어 있지 않고 최소 길이가 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};
}
아시겠지만 VERY UGLY입니다. 그리고 입력 필드에 값이 있는지 여부 만 확인할 수 있습니다. 최소 길이 나 최대 길이를 구할 수 없으며 이와 같은 라이브러리를 사용할 수 없습니다. enter link description here
이미있을 수 있습니다 :
당신이 뭔가를 할 수있는, 필드의 이상이 필요하고, 10보다 큰 부르는 것들은 할 필요하면 코드를 게시 할 수 있습니다 'alternativeRevenueGroupValidator'에 대해서 – pixelbits
이 답변보기 : http://stackoverflow.com/questions/36851837/angular-2-combined-form-validation. 그룹 유효성 검사기가 우선 순위를 결정하면 control.setErrors (null)'을 호출하여 컨트롤 오류를 지울 수 있습니다. 즉, 개별 컨트롤에 유효성 검사기를 가질 수 있습니다. – pixelbits