【发布时间】:2023-03-08 09:25:02
【问题描述】:
我有以下表单设置:
this.form = new FormGroup({
...
year: new FormControl("", validateDateMethod(year, month, day)),
month: new FormControl("", validateDateMethod(year, month, day)),
day: new FormControl("", validateDateMethod(year, month, day))
});
我想创建一个自定义验证器来根据 3 个字段验证日期。
是否可以基于多个表单值创建验证器?
普通海关验证器只接受当前控件和任何参数值。
谢谢
【问题讨论】:
-
请查看此链接中的代码:stackoverflow.com/a/44449802/3875919
-
我已经尝试过类似的方法,但我收到一个错误:Argument of type is not assignable to parameter of type 'ValidatorFn |验证器Fn[] |抽象控制选项'。对象字面量只能指定已知属性,而 'validator' 不存在于类型 'ValidatorFn |验证器Fn[] | AbstractControlOptions
-
我需要使用表单生成器来清除错误
-
您可以通过在
FormControl的errors 对象中设置自定义属性,然后使用*ngIf指令在DOM 中显示错误消息来实现。
标签: angular angular-reactive-forms angular-validation angular-validator