2017-05-25 244 views
2

使用我已经添加了一个ngbDatePicker反应形式:ngBootstrap对角4的DatePicker总是需要

<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] 
    ] 

    // ... 
}); 

但是,如果字段是空的它仍然ngInvalid为ngbDateFormat

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

任何人都知道如何使DatePicker字段允许一个空字符串?

+1

参见:https://github.com/ng-bootstrap/ng-bootstrap/issues/1582#issuecomment-304204567 –

回答

2

答案由@ pkozlowski.opensource通过github issue提供,表明模型必须设置为null以避免验证可选字段。