您好,感谢您抽出宝贵的时间。我有一个控件,希望确认其中是否有至少一项。我相信我需要为此编写一个customvalidator,因为这不同于确保在框中选择了某些东西(是真的吗?我认为内置的required,min或minlength验证器在这里不起作用。 ..am我错了吗?有人告诉我,我很可能需要它返回ValidatorFn。由于我已经尝试了许多方法,因此对此提供的任何帮助将不胜感激。 -杰森
//Don't believe control.get.length === 0 is correct , keeps coming back as 1
CheckThatSelectedCountiesHasValues(): ValidatorFn {
return (control: AbstractControl): { [key: string]: any } => {
return control.get.length === 0 ? { 'needcounties': true } : null
};
}
//component code to attach the validator to the control
createFormControls() {
this.lstcountiesselected = new FormControl('', Validators.compose([this.customValidator.CheckThatSelectedCountiesHasValues()]));
}
//html validator message
<span *ngIf="lstcountiesselected.errors">
<span *ngIf="registerFormControl.lstcountiesselected.errors?.needcounties">
<p class="hasErrorExpl">***County is required***</p>
</span>
</span>