Skip to content
This repository was archived by the owner on Sep 5, 2024. It is now read-only.

datepicker: invalid value on first load from string #12075

Closed
TemitaTom opened this issue Jan 26, 2021 · 6 comments · Fixed by #12115
Closed

datepicker: invalid value on first load from string #12075

TemitaTom opened this issue Jan 26, 2021 · 6 comments · Fixed by #12115
Assignees
Labels
has: Pull Request A PR has been created to address this issue P2: required Issues that must be fixed. resolution: fixed severity: regression This issue is related to a regression type: bug
Milestone

Comments

@TemitaTom
Copy link

TemitaTom commented Jan 26, 2021

Bug Report

Demo and steps to reproduce the issue

This blank StackBlitz demo can be used to create a reproduction that demonstrates your issue.

Demo URL (required)*:

Detailed Reproduction Steps

HTML

<md-content ng-app="MyApp" ng-controller="AppCtrl as ctrl" layout-padding="" layout-margin="" class="datepickerdemoMoment">
  <form name="form1" ng-model="ctrl.form">
  <md-datepicker name="datep" ng-model="ctrl.myDate" ng-change="ctrl.onDateChanged()" md-placeholder="Enter date"></md-datepicker> 
  Date: {{ctrl.myDate | date:shortDate}} {{ form1.datep.$error }}
</md-content>
  </form>
<!--
Copyright 2020 Google LLC. All Rights Reserved. 
Use of this source code is governed by an MIT-style license that can be found
in the LICENSE file at http://material.angularjs.org/HEAD/license.
-->

JS

(function () {
  'use strict';

  angular.module('MyApp', ['ngMaterial', 'ngMessages', 'material.svgAssetsCache']).config(function($mdDateLocaleProvider) {
    /**
     * @param date {Date}
     * @returns {string} string representation of the provided date
     */
    $mdDateLocaleProvider.formatDate = function(date) {
      return date ? moment(date).format('L') : '';
    };

    /**
     * @param dateString {string} string that can be converted to a Date
     * @returns {Date} JavaScript Date object created from the provided dateString
     */
    $mdDateLocaleProvider.parseDate = function(dateString) {
      var m = moment(dateString, 'L', true);
      return m.isValid() ? m.toDate() : new Date(NaN);
    };
  })
  .controller("AppCtrl", function($log) {
    this.myDate = "2021-01-20T07:00:00Z";

    this.onDateChanged = function() {
      $log.log('Updated Date: ', this.myDate);
    };
  });
})();


/**
Copyright 2020 Google LLC. All Rights Reserved. 
Use of this source code is governed by an MIT-style license that can be found
in the LICENSE file at http://material.angularjs.org/HEAD/license.
**/

With this code, the date IS INVALID. In version AMjs 1.2.1 work great.

Explain the expected behavior

Explain the current behavior

Discuss the use-case or motivation for changing the existing behavior

List the affected versions of AngularJS, Material, OS, and browsers

  • AngularJS: 1.8.2
  • AngularJS Material: 1.2.2
  • OS: All
  • Browsers: All
  • Screen Readers:

Add anything else we should know

Stack Trace


Screenshots

@TemitaTom TemitaTom added needs triage Issues that need initial triage type: bug labels Jan 26, 2021
@Splaktar Splaktar self-assigned this Feb 1, 2021
@Splaktar Splaktar added the needs: demo A CodePen demo or GitHub repository is needed to demonstrate the reproduction of the issue label Feb 1, 2021
@Splaktar
Copy link
Member

Splaktar commented Feb 1, 2021

Since a StackBlitz demo (required) was not provided, I had to do some work to get this sample code to run. Here's the StackBlitz demo.

@Splaktar Splaktar removed the needs: demo A CodePen demo or GitHub repository is needed to demonstrate the reproduction of the issue label Feb 1, 2021
@Splaktar
Copy link
Member

Splaktar commented Feb 1, 2021

For now, the quick workaround is doing

ctrl.myDate = new Date("2021-01-20T07:00:00Z");

instead of

ctrl.myDate = "2021-01-20T07:00:00Z";

@Splaktar Splaktar added needs: investigation The cause of this issue is not well understood and needs to be investigated by the team or community P3: important Important issues that really should be fixed when possible. severity: regression This issue is related to a regression and removed needs triage Issues that need initial triage labels Feb 1, 2021
@Splaktar Splaktar added this to the 1.2.3 milestone Feb 1, 2021
@Splaktar Splaktar added P2: required Issues that must be fixed. and removed P3: important Important issues that really should be fixed when possible. labels Apr 18, 2021
@j-walker23
Copy link

FYI, this also fails when the date is type moment.

When DatePickerCtrl.prototype.updateErrorState is called with a moment date
if (this.dateUtil.isValidDate(date)) { is false
So this.ngModelCtrl.$setValidity('valid', date == null); runs and the ngModel ctrl valid key is set to false.

@Splaktar
Copy link
Member

Splaktar commented Jul 5, 2021

It looks like this was caused by b406623.

Splaktar added a commit that referenced this issue Jul 5, 2021
- when in `updateErrorState()`, need to make sure that ISO 8601 strings are
  parsed to dates

Fixes #12075
@Splaktar Splaktar added has: Pull Request A PR has been created to address this issue and removed needs: investigation The cause of this issue is not well understood and needs to be investigated by the team or community labels Jul 5, 2021
@Splaktar
Copy link
Member

Splaktar commented Jul 5, 2021

Fixed in #12115

Splaktar added a commit that referenced this issue Jul 8, 2021
- when in `updateErrorState()`, need to make sure that ISO 8601 strings are
  parsed to dates

Fixes #12075
@j-walker23
Copy link

Dang, this only fixed it for string. Not moment objects.

superheri pushed a commit to superheri/material that referenced this issue Nov 30, 2021
- when in `updateErrorState()`, need to make sure that ISO 8601 strings are
  parsed to dates

Fixes angular#12075
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
has: Pull Request A PR has been created to address this issue P2: required Issues that must be fixed. resolution: fixed severity: regression This issue is related to a regression type: bug
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants