mirror of
https://github.com/jquery/jquery-ui.git
synced 2025-01-07 20:34:24 +00:00
Datepicker: Handle invalid values in value()
and valueAsDate()
This commit is contained in:
parent
b4fe65a134
commit
feedc57486
@ -64,7 +64,7 @@ test( "open", function() {
|
|||||||
});
|
});
|
||||||
|
|
||||||
test( "value", function() {
|
test( "value", function() {
|
||||||
expect( 5 );
|
expect( 6 );
|
||||||
var input = $( "#datepicker" ).datepicker(),
|
var input = $( "#datepicker" ).datepicker(),
|
||||||
picker = input.datepicker( "widget" ),
|
picker = input.datepicker( "widget" ),
|
||||||
inline = $( "#inline" ).datepicker();
|
inline = $( "#inline" ).datepicker();
|
||||||
@ -75,19 +75,21 @@ test( "value", function() {
|
|||||||
"first day marked as selected" );
|
"first day marked as selected" );
|
||||||
equal( input.datepicker( "value" ), "1/1/14", "getter" );
|
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" );
|
inline.datepicker( "value", "1/1/14" );
|
||||||
ok( inline.find( "a[data-timestamp]:first" ).hasClass( "ui-state-focus" ),
|
ok( inline.find( "a[data-timestamp]:first" ).hasClass( "ui-state-focus" ),
|
||||||
"first day marked as selected" );
|
"first day marked as selected" );
|
||||||
equal( inline.datepicker( "value" ), "1/1/14", "getter" );
|
equal( inline.datepicker( "value" ), "1/1/14", "getter" );
|
||||||
|
|
||||||
// TODO: Handle for invalid values.
|
|
||||||
|
|
||||||
input.datepicker( "destroy" );
|
input.datepicker( "destroy" );
|
||||||
inline.datepicker( "destroy" );
|
inline.datepicker( "destroy" );
|
||||||
});
|
});
|
||||||
|
|
||||||
test( "valueAsDate", function() {
|
test( "valueAsDate", function() {
|
||||||
expect( 5 );
|
expect( 6 );
|
||||||
var input = $( "#datepicker" ).datepicker(),
|
var input = $( "#datepicker" ).datepicker(),
|
||||||
picker = input.datepicker( "widget" ),
|
picker = input.datepicker( "widget" ),
|
||||||
inline = $( "#inline" ).datepicker();
|
inline = $( "#inline" ).datepicker();
|
||||||
@ -99,14 +101,15 @@ test( "valueAsDate", function() {
|
|||||||
TestHelpers.datepicker.equalsDate( input.datepicker( "valueAsDate" ),
|
TestHelpers.datepicker.equalsDate( input.datepicker( "valueAsDate" ),
|
||||||
new Date( 2014, 0, 1 ), "getter" );
|
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 ) );
|
inline.datepicker( "valueAsDate", new Date( 2014, 0, 1 ) );
|
||||||
ok( inline.find( "a[data-timestamp]:first" ).hasClass( "ui-state-focus" ),
|
ok( inline.find( "a[data-timestamp]:first" ).hasClass( "ui-state-focus" ),
|
||||||
"first day marked as selected" );
|
"first day marked as selected" );
|
||||||
TestHelpers.datepicker.equalsDate( inline.datepicker( "valueAsDate" ),
|
TestHelpers.datepicker.equalsDate( inline.datepicker( "valueAsDate" ),
|
||||||
new Date( 2014, 0, 1 ), "getter" );
|
new Date( 2014, 0, 1 ), "getter" );
|
||||||
|
|
||||||
// TODO: Handle for invalid values.
|
|
||||||
|
|
||||||
input.datepicker( "destroy" );
|
input.datepicker( "destroy" );
|
||||||
inline.datepicker( "destroy" );
|
inline.datepicker( "destroy" );
|
||||||
});
|
});
|
||||||
|
@ -644,17 +644,20 @@ $.widget( "ui.datepicker", {
|
|||||||
if ( arguments.length ) {
|
if ( arguments.length ) {
|
||||||
this._value( value );
|
this._value( value );
|
||||||
} else {
|
} else {
|
||||||
return this.date.format();
|
return this.isValid() ? this.date.format() : this.element.val();
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
valueAsDate: function( value ) {
|
valueAsDate: function( value ) {
|
||||||
if ( arguments.length ) {
|
if ( arguments.length ) {
|
||||||
this._value( value );
|
this._value( value );
|
||||||
} else {
|
} else {
|
||||||
return this.date.date();
|
return this.isValid() ? this.date.date() : null;
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
isValid: function() {
|
isValid: function() {
|
||||||
|
if ( this.inline ) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
return Globalize.parseDate( this.element.val(), this.options.dateFormat ) !== null;
|
return Globalize.parseDate( this.element.val(), this.options.dateFormat ) !== null;
|
||||||
},
|
},
|
||||||
_destroy: function() {
|
_destroy: function() {
|
||||||
|
Loading…
Reference in New Issue
Block a user