【问题标题】:ngBootstrap for Angular 4 DatePicker in a Reactive Form is Always Required始终需要反应形式的 Angular 4 DatePicker 的 ngBootstrap
【发布时间】:2017-05-25 23:17:25
【问题描述】:

使用响应式表单我添加了 ngbDatePicker:

<input type="text"
        id="business_incorp_date"
        class="form-control"
        formControlName="business_incorp_date"
        ngbDatepicker
        #incorporatedDatePicker="ngbDatepicker"
        (click)="incorporatedDatePicker.toggle()"
        readonly>

对于表单模型:

this.form = this.formBuilder.group({
  id: [
    0, [Validators.required]
  ],

  // ... removed for brevity

  business_type: [
    '', [Validators.required]
  ],
  business_incorp_date: [
    '', [FormValidators.date] // <-- NOT REQUIRED BY MODEL
  ],
  business_desc: [
    '', [Validators.required]
  ]

  // ...
});

但是,如果该字段为空,对于 ngbDateFormat,它仍然是 ngInvalid:

{ "ngbDate": { "invalid": { "year": null, "month": null, "day": null } } }

有人知道如何让 DatePicker 字段允许空字符串吗?

【问题讨论】:

标签: angular datepicker ng-bootstrap


【解决方案1】:

@pkozlowski.opensource 通过github issue 提供的答案指出必须将模型设置为 null 以避免对可选字段进行验证。

【讨论】:

  • 感谢您的评论,我已经浪费了很多时间试图找到正确的方法。很简单!
猜你喜欢
  • 2018-01-16
  • 1970-01-01
  • 1970-01-01
  • 2017-12-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-11-30
  • 2023-03-21
相关资源
最近更新 更多