Skip to content
This repository has been archived by the owner on May 29, 2019. It is now read-only.

Datepicker clicking clear button on popup gives "ng-model" value must be a Date object #3701

Closed
gjmoyer opened this issue May 22, 2015 · 7 comments

Comments

@gjmoyer
Copy link

gjmoyer commented May 22, 2015

If you have a Datepicker directive as a popup, and choose a valid date, then popup again and click the clear button, you get the following exception:
Datepicker directive: "ng-model" value must be a Date object, a number of milliseconds since 01.01.1970 or a string representing an RFC2822 or ISO 8601 date.

This then leaves the form.$invalid property as true, which is not correct, if this is not a required field.

Open the console window on the demo page, and follow the above 2 steps, and you will see the error emitted to console window.

@rvanbaalen rvanbaalen added this to the Backlog milestone May 22, 2015
@rvanbaalen
Copy link
Contributor

Reproducable in this plunkr. Doesn't look like desired behaviour indeed.

http://plnkr.co/edit/Ok61ylnLCle9oQAFTv4o?p=preview

@dkavun
Copy link

dkavun commented May 26, 2015

Looks like the problem is either in $viewChangeListener or in the dateParser: 'clear' passes an empty string as a selected value and the dateParser parses it to 'Invalid date'. It seems to me, that null should be returned in this case.

@gjmoyer
Copy link
Author

gjmoyer commented May 27, 2015

In datepicker.js line 652, I changed the code to this, and it appears to fix the issue.
ngModel.$viewChangeListeners.push(function () {
if (ngModel.$viewValue) scope.date = dateParser.parse(ngModel.$viewValue, dateFormat, scope.date) || new Date(ngModel.$viewValue);
else scope.date = null;
});

@rvanbaalen rvanbaalen mentioned this issue Jun 1, 2015
@chrisirhc chrisirhc modified the milestones: 0.13.x, Backlog Jun 2, 2015
@wesleycho wesleycho modified the milestones: 0.13.1 (Performance), 0.13.x Jun 4, 2015
@slavafomin
Copy link

This is a very annoying bug indeed. Is there a temporary workaround for this that we can use externally?

Thanks!

@ruionwriting
Copy link

Reverted back to 0.12.0 for now.

@DeWet-vanRooyen
Copy link

I still get this error when I use the format of 'dd/MM/yyyy'

when manually typing in the date and starting with a number greater than 12 it wont work.
Any ideas ? Thanks !

@ImMrLucky
Copy link

I am guessing this is happening because there is no date present on the page when it loads. What I ended up doing was setting the value to undefined instead of null if there is no value for that field. If there is a value then I convert it to a new Date object. I hope this code helps. I believe that the value cannot be null but it can be undefined. In my case I was setting the value as it was returned from the JSON object in my service like this:
_.each(_estimate.EstimateLineItems, function(li){
if(li.IssueDate == null) {
li.IssueDate = undefined;
} else {
li.IssueDate = new Date(li.IssueDate);
}
});

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

9 participants