ui-date icon indicating copy to clipboard operation
ui-date copied to clipboard

Be more forgiving with empty strings in ng-model expressions

Open steinerj opened this issue 12 years ago • 3 comments

          // Update the date picker when the model changes
          controller.$render = function () {
              var date = controller.$viewValue;
            if ( angular.isDefined(date) && date !== null && !angular.isDate(date) ) {
              throw new Error('ng-Model value must be a Date object - currently it is a ' + typeof date + ' - use ui-date-format to convert it from a string');
            }
            element.datepicker("setDate", date);
          };

Apologies to start off with a code block. I've read that this part is intentionally strict in what it accepts (Date objects, null or undefined). However, when you define a ng-model, e.g. on an input, it defaults to empty string (""). In this case one has to define the value explicitly to null or undefined (in controller or via ng-init), otherwise it will throw the exception. Wouldn't it be reasonable to be more forgiving and treat emtpy strings like nulls in this place?

steinerj avatar May 22 '13 15:05 steinerj

Can you provide a plunker that demonstrates this issue? The $render should only be called when you update the model value programmatically.

petebacondarwin avatar Jun 03 '13 09:06 petebacondarwin

I think I'm having a similar issue... my initial ng-model value is not shown in the text field, I fear because it comes back from my server in the form: 2015-01-09 12:00:00 AM

My input field looks like this:

<input ui-date ui-date-format="yyyy-MM-dd" name="effectiveDate" ng-model="squad.effectiveDate" />

I should also add that the date selected does not display in the format I've specified.

JaKXz avatar Jan 09 '15 19:01 JaKXz

@JaKXz ui-date doesn't currently handle multiple formats that well unless custom parsers are defined. If it is really coming from the server in a different format, for now the easiest way would be to convert it to the date format you are expecting before using it.

alexanderchan avatar Jan 12 '15 09:01 alexanderchan