如何验证彼此依赖的Angular2表单控件? [英] How to validate Angular2 form controls that depend on eachother?

查看:61
本文介绍了如何验证彼此依赖的Angular2表单控件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试创建一个绑定到反应形式控件的时间选择器组件.如果结束时间早于开始时间,我希望表单控件无效.我知道Angular附带的内置验证器,但是我希望能够针对另一个表单控件进行验证.如果有人可以指出正确的方向,那将是很好的.

I am trying to create a time picker component that binds to a reactive form control. I want the form control to be invalid if the end time is before the start time. I am aware of the built in validators that Angular comes with, but I want to be able to validate against another form control. If someone could point me in the right direction that would be great.

推荐答案

这是我采用的方法:

  • 在包含两个控件的FormGroup上定义验证器.
  • 在验证器中,使用传入的FormGroup对象检索两个FormControl对象的值并执行验证.
  • Define the validator on the FormGroup containing the two controls.
  • In the validator, use the FormGroup object that is passed in to retrieve the values of the two FormControl objects and perform the validation.

这篇关于如何验证彼此依赖的Angular2表单控件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆