Skip to content
This repository was archived by the owner on Sep 5, 2024. It is now read-only.
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

Description

@TemitaTom

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

Activity

  1. self-assigned this
    on Feb 1, 2021
  2. added
    needs: demoA CodePen demo or GitHub repository is needed to demonstrate the reproduction of the issue
    on Feb 1, 2021
  3. Splaktar commented on Feb 1, 2021

    @Splaktar
    Contributor

    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.

  4. removed
    needs: demoA CodePen demo or GitHub repository is needed to demonstrate the reproduction of the issue
    on Feb 1, 2021
  5. Splaktar commented on Feb 1, 2021

    @Splaktar
    Contributor

    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";
  6. added
    needs: investigationThe cause of this issue is not well understood and needs to be investigated by the team or community
    P3: importantImportant issues that really should be fixed when possible.
    and removed
    needs triageIssues that need initial triage
    on Feb 1, 2021
  7. added this to the 1.2.3 milestone on Feb 1, 2021
  8. added
    P2: requiredIssues that must be fixed.
    and removed
    P3: importantImportant issues that really should be fixed when possible.
    on Apr 18, 2021
  9. j-walker23 commented on Jun 8, 2021

    @j-walker23

    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.

  10. Splaktar commented on Jul 5, 2021

    @Splaktar
    Contributor

    It looks like this was caused by b406623.

  11. added a commit that references this issue on Jul 5, 2021
    2c2cf6b
  12. added
    has: Pull RequestA PR has been created to address this issue
    and removed
    needs: investigationThe cause of this issue is not well understood and needs to be investigated by the team or community
    on Jul 5, 2021
  13. Splaktar commented on Jul 5, 2021

    @Splaktar
    Contributor

    Fixed in #12115

  14. added a commit that references this issue on Jul 8, 2021
    0a06f99
  15. j-walker23 commented on Aug 6, 2021

    @j-walker23

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

  16. added a commit that references this issue on Nov 30, 2021
    eca7f99
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions