question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Feature: ngbDatePicker ngInvalid for Optional Field in Reactive Form

See original GitHub issue

Feature description:

There’s no way to have a date field be optional when using ngbDatePicker it always indicates the field is ngInvalid. I tried setting the model to '', null, undefined. Empty string means ngInvalid will prevent it from being optional, and null/undefined works to make it optional, but it throws an error when null/undefined is split by formatDate.

private formatDate(date: string): { year: number, month: number, day: number } {
  const { 0: year, 1: month, 2: day } = date.split('-'); 
  return { year: parseInt(year, 10), month: parseInt(month, 10), day: parseInt(day, 10) };
}

Looks like this might just need a null check of some sort in formatDate, but it would be a better if it instead relied on Validations.required to be set by the developer instead so whether it is required is external to the directive making an empty string valid.

Example Form Model

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

  // ... removed for brevity

  business_type: [
    '', [Validators.required]
  ],
  business_incorp_date: [
    '', [] // <-- OPTIONAL FIELD, but always ngInvalid on { "ngbDate": { "invalid": { "year": null, "month": null, "day": null } } }
  ],
  business_desc: [
    '', [Validators.required]
  ]

  // ...
});

Example Input

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

Version of Angular, ng-bootstrap, and Bootstrap:

Angular: 4.1.1

ng-bootstrap: ^1.0.0-alpha.25

Bootstrap: 4.0.0-alpha.6

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Reactions:3
  • Comments:8 (1 by maintainers)

github_iconTop GitHub Comments

13reactions
pkozlowski-opensourcecommented, May 26, 2017

It is because you are initialising it to an empty string here:

business_incorp_date: [
    '', [] // <-- OPTIONAL FIELD, but always ngInvalid on { "ngbDate": { "invalid": { "year": null, "month": null, "day": null } } }
  ]

It should be null instead:

business_incorp_date: [
    null, []
  ]

I’ve got it working just fine here: plnkr.co/edit/bdque81d3BO1ZcsoDhc0?p=preview.

If it still doesn’t work for you please provide a reproduce scenario in a plunker. It is not actionable if we can’t reproduce it: https://github.com/ng-bootstrap/ng-bootstrap#you-think-youve-found-a-bug

3reactions
dabichocommented, Jun 16, 2017

I found this while searching for the same behaviour.

Here is a plunkr derived from examples with reactive forms http://plnkr.co/edit/vgLDwtcBymGVoMr0WDbE?p=preview

The behaviour I was going to is, if the input is empty, then it should be valid (meaning if I manually delete the content and switch focus, it should be valid, or if ngbDatepicker clears the input, then the form element should be valid…). Can I achieve that without a button?

One way I can think of is to listen the input for changes, and if the input is empty, then clear the calendar.

Would you think this is clean?

Read more comments on GitHub >

github_iconTop Results From Across the Web

ngBootstrap for Angular 4 DatePicker in a Reactive Form is ...
opensource via github issue that states that the model must be set to null to avoid validation on optional fields. Share.
Read more >
How to Reactive Form in Angular DatePicker component
DatePicker is a form component and validation is playing vital role in forms to get the valid data. Here to showcase the DatePicker...
Read more >
Angular Date and Time picker Reactive forms required validat
Hi all, I have been trying to fix Date and Time picker required validation. I cannot get hold of touched and required validation...
Read more >
Ngb Datepicker Validation Opt Out - StackBlitz
Example usage of the datepicker widget from https://ng-bootstrap.github.io.
Read more >
Datepicker - Angular powered Bootstrap
Manages interaction with the input field itself, does value formatting and provides forms integration. Selector: 'input[ngbDatepicker]'. Exported as: ' ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found