2017-05-25 242 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以避免驗證可選字段。