Edit: I'm just going with the workaround. Sorry for the bother. (have another issue regarding this, though. I'll post it separately).
In an edit form the date input field is set to a value from the database. When the datepicker is opened and closed (by clicking outside the datepicker area) without selecting a date, the value in the date input field gets changed to nothing. The desired behavior would be that it stays on the value it was set to beforehand. Is it possible to prevent the datpicker from updating the field when no date is selected?
(or maybe as a workaround, i set the datepicker date to the previously set value)...
I used this workaround,
$('.datepicker').datepicker({
format: "D M d, yyyy",
autoclose: true,
todayHighlight: true
}).on('show', function(e) {
if($(this).val().length > 0) {
$(this).datepicker('update', new Date($(this).val()));
}
});
This workaround could easily be integrated in the datepicker, do you maybe want to send a PR for that @boriscosic?
had the same problem, using this workaround as well
I've been really busy but I will create a pull request for this. @Jrizzi1 there is a bug in my code that breaks prev/next so use this version. If the datepicker is visible it will not try and set the date as Prev/Next month triggers show for me.
$('.datepicker').datepicker({
format: "D M d, yyyy",
autoclose: true,
todayHighlight: true
}).on('show', function(e) {
if($(this).val().length > 0 && $('.datepicker:visible') == false) {
$(this).datepicker('update', new Date($(this).val()));
}
});
@boriscosic that last snippet still clears the field if no date is selected, however the next/prev buttons work again
I had the same issue - when nothing is selected (click outside the datepicker) the target value would change.
It turned out that this behaviour is controlled by the option forceParse, which defaults to true.
Explicitly setting the forceParse to false results in the expected behaviour, i.e. if you click outside of datepicker this will be interpreted as Cancel and will not set the target widget.
So IMHO there is no need to make any changes to the source code of datepicker. The only change necessary is in the datepicker option documentation, which needs to mention this side effect.
Hi All,
As per given suggestion by @miljenko1
set forceParse to false
, it has a default value to true that's why it validates date picker every time and update its value to today date or default date.
I was facing the same issue, but i came to know it only happens when you set the date as yyyy-mm-dd and in datepicker format was dd-mm-yyyy in javascript. So this was causing the problem.
Now i made both as dd-mm-yyyy. the issue vanished. Hope it will be helpful to someone.
I solved this issue using the letter P in the end of format options, like this:
$("#send_at").datetimepicker({
format: 'dd/mm/yyyy hh:ii:ssP',
[...]
});
In this case, I used format to pt-br, but probaly works in another date formats.
Most helpful comment
I had the same issue - when nothing is selected (click outside the datepicker) the target value would change.
It turned out that this behaviour is controlled by the option
forceParse, which defaults to true.Explicitly setting the
forceParseto false results in the expected behaviour, i.e. if you click outside of datepicker this will be interpreted as Cancel and will not set the target widget.So IMHO there is no need to make any changes to the source code of datepicker. The only change necessary is in the datepicker option documentation, which needs to mention this side effect.