我使用角4形式,我有一些领域。和first_name,last_name和公司对我来说非常重要。我想强制用户填写这3个字段中的一个。我该怎么做?
这里是.ts代码:
this.contactForm = this.fb.group({
first_name: [null, Validators.compose([Validators.required])],
last_name: [null, Validators.compose([Validators.required])],
email: [null, Validators.compose([this.validateEmail])],
company: [null],
position: [null],
});
一个html:
<form [formGroup]="contactForm" fxLayout="column">
<md-input-container class="data_light">
<input class="data_font capital" mdInput placeholder="{{'Contacts.FirstName' | translate}}" [formControl]="contactForm.controls['first_name']">
</md-input-container>
<small *ngIf="contactForm.controls['first_name'].hasError('required') && contactForm.controls['first_name'].touched" class="mat-text-warn data_light">{{'Contacts.firstNameReq' | translate}}
</small>
<md-input-container class="data_light">
<input class="data_font capital" mdInput placeholder="{{'Contacts.lastName' | translate}}" [formControl]="contactForm.controls['last_name']">
</md-input-container>
<small *ngIf="contactForm.controls['last_name'].hasError('required') && contactForm.controls['last_name'].touched" class="mat-text-warn data_light">{{'Contacts.lastNameReq' | translate}}
</small>
<md-input-container class="data_light">
<input class="data_font" mdInput placeholder="{{'Contacts.email' | translate}}" [formControl]="contactForm.controls['email']"
(blur)="checkContactEmail()">
</md-input-container>
<small *ngIf="contactForm.controls['email'].hasError('validateEmail') && contactForm.controls['email'].dirty" class="mat-text-warn data_light">{{'Contacts.emailValid' | translate}}
</small>
<small *ngIf="!emailValid" class="mat-text-warn data_light">{{emailMessage}}
</small>
<md-input-container class="data_light">
<input class="data_font capital" mdInput placeholder="{{'Contacts.Company' | translate}}" [formControl]="contactForm.controls['company']">
</md-input-container>
<md-input-container class="data_light">
<input class="data_font capital" mdInput placeholder="{{'Contacts.Position' | translate}}" [formControl]="contactForm.controls['position']">
</md-input-container>
</form>
答案 0 :(得分:6)
private atLeastOneValidator = () => {
return (controlGroup) => {
let controls = controlGroup.controls;
if ( controls ) {
let theOne = Object.keys(controls).find(key=> controls[key].value!=='');
if ( !theOne ) {
return {
atLeastOneRequired : {
text : 'At least one should be selected'
}
}
}
}
return null;
};
};
上面是一个可重复使用的验证器,您可以像这样使用它:
this.contactForm.setValidators(this.atLeastOneValidator())
如果没有任何字段具有值,则会使整个contactForm
无效。
请注意,contactForm
的默认验证仍然可以很好地运行,您不必关心表单中的字段数量,并且所有内容都是动态处理的
编辑:
请注意,atLeastOneValidator
正在检查值是否为空,但如果您想说:
其中至少有一个字段必须为valid
,那么您只需将条件调整到下面
let theOne = Object.keys(controls).find(key=> controls[key].valid );
然后,您可以在模板中使用错误,如下所示:
<small *ngIf="contactForm.hasError('atLeastOneRequired')" class="mat-text-warn data_light">{{contactForm.getError('atLeastOneRequired')}}
</small>
答案 1 :(得分:0)
禁用按钮,直到所需字段不是用户
<button type='submit' [disabled]='!contactForm.valid'> Submit</button>
然后调用函数来检查这样的禁用
<button type='submit' [disabled]='checkValid()'> Submit</button>
checkValid() {
if(this.contactForm.get('first_name').valid || this.contactForm.get('last_name').valid || this.contactForm.get('email').valid) {
return false;
} else {
return true;
}
}
答案 2 :(得分:0)
您可以使用以下验证器
AtLeastOneInputHasValue = () => {
return (group: FormGroup) => {
if (!Object.values(group.value).find(value => value !== '')) {
return { message: 'Please input at least one value' }
}
return null
}
}
并将其添加到组验证器中
this.contactForm = this.fb.group({
...
},
this.AtLeastOneInputHasValue()
);
在模板上,如果表单上的任何验证器无效,请在按钮上使用disabled
<button type='submit' [disabled]='!contactForm.valid'> Submit</button>