fix datefield (datetimefield) to return null for incorrect dates
This commit is contained in:
parent
9cbcb0e99c
commit
a00dac75ca
@ -3,6 +3,7 @@ X-editable changelog
|
|||||||
|
|
||||||
Version 1.4.5 wip
|
Version 1.4.5 wip
|
||||||
----------------------------
|
----------------------------
|
||||||
|
[bug] fix datefield (datetimefield) to return null for incorrect dates (vitalets)
|
||||||
[bug #224] do not close popup when it is saving value (vitalets)
|
[bug #224] do not close popup when it is saving value (vitalets)
|
||||||
[enh] added `submitValue` to `save` event params (vitalets)
|
[enh] added `submitValue` to `save` event params (vitalets)
|
||||||
[enh #259] allow `getValue` method to return value itself, not object (vitalets)
|
[enh #259] allow `getValue` method to return value itself, not object (vitalets)
|
||||||
|
@ -83,7 +83,7 @@ $(function(){
|
|||||||
},
|
},
|
||||||
|
|
||||||
html2value: function(html) {
|
html2value: function(html) {
|
||||||
return html ? this.dpg.parseDate(html, this.parsedViewFormat, this.options.datepicker.language) : null;
|
return this.parseDate(html, this.parsedViewFormat);
|
||||||
},
|
},
|
||||||
|
|
||||||
value2str: function(value) {
|
value2str: function(value) {
|
||||||
@ -91,7 +91,7 @@ $(function(){
|
|||||||
},
|
},
|
||||||
|
|
||||||
str2value: function(str) {
|
str2value: function(str) {
|
||||||
return str ? this.dpg.parseDate(str, this.parsedFormat, this.options.datepicker.language) : null;
|
return this.parseDate(str, this.parsedFormat);
|
||||||
},
|
},
|
||||||
|
|
||||||
value2submit: function(value) {
|
value2submit: function(value) {
|
||||||
@ -136,6 +136,25 @@ $(function(){
|
|||||||
}, 200);
|
}, 200);
|
||||||
});
|
});
|
||||||
*/
|
*/
|
||||||
|
},
|
||||||
|
|
||||||
|
/*
|
||||||
|
For incorrect date bootstrap-datepicker returns current date that is not suitable
|
||||||
|
for datefield.
|
||||||
|
This function returns null for incorrect date.
|
||||||
|
*/
|
||||||
|
parseDate: function(str, format) {
|
||||||
|
var date = null, formattedBack;
|
||||||
|
if(str) {
|
||||||
|
date = this.dpg.parseDate(str, format, this.options.datepicker.language);
|
||||||
|
if(typeof str === 'string') {
|
||||||
|
formattedBack = this.dpg.formatDate(date, format, this.options.datepicker.language);
|
||||||
|
if(str !== formattedBack) {
|
||||||
|
date = null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return date;
|
||||||
}
|
}
|
||||||
|
|
||||||
});
|
});
|
||||||
|
@ -101,7 +101,7 @@ $(function(){
|
|||||||
|
|
||||||
html2value: function(html) {
|
html2value: function(html) {
|
||||||
//parseDate return utc date!
|
//parseDate return utc date!
|
||||||
var value = html ? this.dpg.parseDate(html, this.parsedViewFormat, this.options.datetimepicker.language, this.options.formatType) : null;
|
var value = this.parseDate(html, this.parsedViewFormat);
|
||||||
return value ? this.fromUTC(value) : null;
|
return value ? this.fromUTC(value) : null;
|
||||||
},
|
},
|
||||||
|
|
||||||
@ -112,7 +112,7 @@ $(function(){
|
|||||||
|
|
||||||
str2value: function(str) {
|
str2value: function(str) {
|
||||||
//parseDate return utc date!
|
//parseDate return utc date!
|
||||||
var value = str ? this.dpg.parseDate(str, this.parsedFormat, this.options.datetimepicker.language, this.options.formatType) : null;
|
var value = this.parseDate(str, this.parsedFormat);
|
||||||
return value ? this.fromUTC(value) : null;
|
return value ? this.fromUTC(value) : null;
|
||||||
},
|
},
|
||||||
|
|
||||||
@ -160,6 +160,25 @@ $(function(){
|
|||||||
//convert date from utc to local
|
//convert date from utc to local
|
||||||
fromUTC: function(value) {
|
fromUTC: function(value) {
|
||||||
return value ? new Date(value.valueOf() + value.getTimezoneOffset() * 60000) : value;
|
return value ? new Date(value.valueOf() + value.getTimezoneOffset() * 60000) : value;
|
||||||
|
},
|
||||||
|
|
||||||
|
/*
|
||||||
|
For incorrect date bootstrap-datetimepicker returns current date that is not suitable
|
||||||
|
for datetimefield.
|
||||||
|
This function returns null for incorrect date.
|
||||||
|
*/
|
||||||
|
parseDate: function(str, format) {
|
||||||
|
var date = null, formattedBack;
|
||||||
|
if(str) {
|
||||||
|
date = this.dpg.parseDate(str, format, this.options.datetimepicker.language, this.options.formatType);
|
||||||
|
if(typeof str === 'string') {
|
||||||
|
formattedBack = this.dpg.formatDate(date, format, this.options.datetimepicker.language, this.options.formatType);
|
||||||
|
if(str !== formattedBack) {
|
||||||
|
date = null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return date;
|
||||||
}
|
}
|
||||||
|
|
||||||
});
|
});
|
||||||
|
@ -105,4 +105,31 @@ $(function () {
|
|||||||
equal(e.text(), dview, 'text correct');
|
equal(e.text(), dview, 'text correct');
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
||||||
|
test("incorrect date", function () {
|
||||||
|
var d = '15.05.1984',
|
||||||
|
e = $('<a href="#" data-type="date" data-pk="1">'+d+'</a>').appendTo('#qunit-fixture').editable({
|
||||||
|
format: f,
|
||||||
|
viewformat: f
|
||||||
|
}),
|
||||||
|
nextD = '16.05.1984';
|
||||||
|
|
||||||
|
equal(frmt(e.data('editable').value, 'dd.mm.yyyy'), d, 'value correct');
|
||||||
|
|
||||||
|
e.click();
|
||||||
|
var p = tip(e);
|
||||||
|
ok(p.find('input').is(':visible'), 'input exists');
|
||||||
|
|
||||||
|
equal(p.find('input').val(), d, 'date set correct');
|
||||||
|
|
||||||
|
//enter incorrect date
|
||||||
|
p.find('input').val('abcde');
|
||||||
|
|
||||||
|
//submit
|
||||||
|
p.find('form').submit();
|
||||||
|
|
||||||
|
ok(!p.is(':visible'), 'popover closed');
|
||||||
|
equal(e.data('editable').value, null, 'date set to null');
|
||||||
|
equal(e.text(), $.fn.editable.defaults.emptytext , 'emptytext shown');
|
||||||
|
});
|
||||||
});
|
});
|
@ -125,4 +125,24 @@ $(function () {
|
|||||||
equal(e.text(), dview, 'text correct');
|
equal(e.text(), dview, 'text correct');
|
||||||
});
|
});
|
||||||
|
|
||||||
|
test("incorrect datetime", function () {
|
||||||
|
var dview = '15/05/1984 15:45',
|
||||||
|
d = '1984-05-15 15:45',
|
||||||
|
e = $('<a href="#" data-type="datetime" data-pk="1" data-format="yyyy-mm-dd hh:ii" data-viewformat="dd/mm/yyyy hh:ii" data-value="'+d+'"></a>').appendTo('#qunit-fixture').editable();
|
||||||
|
|
||||||
|
e.click();
|
||||||
|
var p = tip(e);
|
||||||
|
ok(p.find('input').is(':visible'), 'input exists');
|
||||||
|
|
||||||
|
//enter incorrect date
|
||||||
|
p.find('input').val('abcde');
|
||||||
|
|
||||||
|
//submit
|
||||||
|
p.find('form').submit();
|
||||||
|
|
||||||
|
ok(!p.is(':visible'), 'popover closed');
|
||||||
|
equal(e.data('editable').value, null, 'date set to null');
|
||||||
|
equal(e.text(), $.fn.editable.defaults.emptytext , 'emptytext shown');
|
||||||
|
});
|
||||||
|
|
||||||
});
|
});
|
Loading…
x
Reference in New Issue
Block a user