即使未选择任何值也如何标记ngbDatepicker在表单中有效 [英] How to mark ngbDatepicker Valid in form even when no value is selected

查看:97
本文介绍了即使未选择任何值也如何标记ngbDatepicker在表单中有效的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在Angular2项目的反应形式中使用ng-bootstrap的ngbDatepicker,日期是可选的,但是ngbDatepicker始终将表单标记为无效,除非选择了日期.

I'm making use of ng-bootstrap's ngbDatepicker in a Reactive Form in an Angular2 project, and the dates are optional, however ngbDatepicker always marks the form as Invalid unless a date is selected.

是否可以从表单验证中排除ngbDatepicker或对其进行配置,以使其无论是否设置了值都返回Valid?

Is there a way to exclude ngbDatepicker from the form validation, or to configure it so that it returns Valid whether it has a value set or not?

推荐答案

问题在于我如何初始化表单值.我将默认值设置为空字符串,而我应该将日期初始化为null.所以我正在这样做:

The issue was with how I was initializing the form values. I was setting the default values to empty strings where I should have been initializing the dates to null. So I was doing this:

  replicantForm = this.fb.group({
      name: ['', Validators.required],
      incepdate: '',
      longevity: ''
  })

当我应该这样做时:

  replicantForm = this.fb.group({
      name: ['', Validators.required],
      incepdate: null,
      longevity: ''
  })

非常感谢@ pkozlowski-opensource的澄清.

Many thanks to @pkozlowski-opensource for the clarification.

这篇关于即使未选择任何值也如何标记ngbDatepicker在表单中有效的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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