]> source.dussan.org Git - jquery-ui.git/commitdiff
Datepicker: Handle invalid values in `value()` and `valueAsDate()`
authorTJ VanToll <tj.vantoll@gmail.com>
Fri, 3 Jan 2014 14:16:03 +0000 (09:16 -0500)
committerScott González <scott.gonzalez@gmail.com>
Thu, 29 Jan 2015 22:47:50 +0000 (17:47 -0500)
tests/unit/datepicker/datepicker_methods.js
ui/datepicker.js

index 98034dacdf14b361acc0c9c4364a8ea42154f2e6..281a81c8d9b064737759361d2129d7d613a8d7c4 100644 (file)
@@ -64,7 +64,7 @@ test( "open", function() {
 });
 
 test( "value", function() {
-       expect( 5 );
+       expect( 6 );
        var input = $( "#datepicker" ).datepicker(),
                picker = input.datepicker( "widget" ),
                inline = $( "#inline" ).datepicker();
@@ -75,19 +75,21 @@ test( "value", function() {
                "first day marked as selected" );
        equal( input.datepicker( "value" ), "1/1/14", "getter" );
 
+       input.val( "abc" );
+       equal( input.datepicker( "value" ), "abc",
+               "Invalid values should be returned without formatting." );
+
        inline.datepicker( "value", "1/1/14" );
        ok( inline.find( "a[data-timestamp]:first" ).hasClass( "ui-state-focus" ),
                "first day marked as selected" );
        equal( inline.datepicker( "value" ), "1/1/14", "getter" );
 
-       // TODO: Handle for invalid values.
-
        input.datepicker( "destroy" );
        inline.datepicker( "destroy" );
 });
 
 test( "valueAsDate", function() {
-       expect( 5 );
+       expect( 6 );
        var input = $( "#datepicker" ).datepicker(),
                picker = input.datepicker( "widget" ),
                inline = $( "#inline" ).datepicker();
@@ -99,14 +101,15 @@ test( "valueAsDate", function() {
        TestHelpers.datepicker.equalsDate( input.datepicker( "valueAsDate" ),
                new Date( 2014, 0, 1 ), "getter" );
 
+       input.val( "a/b/c" );
+       equal( input.datepicker( "valueAsDate" ), null, "Invalid dates return null" );
+
        inline.datepicker( "valueAsDate", new Date( 2014, 0, 1 ) );
        ok( inline.find( "a[data-timestamp]:first" ).hasClass( "ui-state-focus" ),
                "first day marked as selected" );
        TestHelpers.datepicker.equalsDate( inline.datepicker( "valueAsDate" ),
                new Date( 2014, 0, 1 ), "getter" );
 
-       // TODO: Handle for invalid values.
-
        input.datepicker( "destroy" );
        inline.datepicker( "destroy" );
 });
index 9f0e1905e8254167d3091315a20d01459de7e6f3..8711b5329d0ced75f6a90df0ea58866f36c82d1b 100644 (file)
@@ -644,17 +644,20 @@ $.widget( "ui.datepicker", {
                if ( arguments.length ) {
                        this._value( value );
                } else {
-                       return this.date.format();
+                       return this.isValid() ? this.date.format() : this.element.val();
                }
        },
        valueAsDate: function( value ) {
                if ( arguments.length ) {
                        this._value( value );
                } else {
-                       return this.date.date();
+                       return this.isValid() ? this.date.date() : null;
                }
        },
        isValid: function() {
+               if ( this.inline ) {
+                       return true;
+               }
                return Globalize.parseDate( this.element.val(), this.options.dateFormat ) !== null;
        },
        _destroy: function() {