Closed CasperWSchmidt closed 11 years ago
Hi Casper, I'd be happy to look into this problem. Could you post a jsFiddle that demonstrates the problem? I'm not able to run the code you posted above because it contains server-side variables like <% =getMinTime() %>
.
I can't see anywhere to save my jsfiddle so you get it here (also I don't know which external URL to use for incudling TImePicker in jsfiddle): // Create timepicker $('#StartTime').timepicker({ forceRoundTime: true, minTime: '08:00', maxTime: '17:00', step: '5', timeFormat: 'H:i' }); $('#StartTime').bind('timeFormatError timeRangeError', function() { alert('Please choose a valid time between 08:00 and 17:00'); $('#StartTime').focus(); });
and for the non-functioning workaround: // Create timepicker $('#StartTime').timepicker({ forceRoundTime: true, minTime: '08:00', maxTime: '17:00', step: '5', timeFormat: 'H:i' }); $('#StartTime').bind('timeFormatError timeRangeError', function() { alert('Please choose a valid time between 08:00 and 17:00'); $('#StartTime').val($('#StartTimeHidden').val()); $('#StartTime').focus(); }); $('#StartTime').bind('changeTime', function() { $('#StartTimeHidden').val($('#StartTime').val()); });
Thanks! It should be fixed now - check the latest version of the code.
Thanks. It fixes the problem first mentioned but the last one persists. I think the changeTime event should also be fired when manually entering a value?
The changeTime even came up in #123 - take a look there for an explanation of why it works the way it does.
I understand why the event should not fire on all changes, but I think my usecase shows a need for it to fire whenever a valid time is typed in (ie. Whenever no error event is fired). onChange is not of much use for me since I don't want to store an invalid value in the hidden since this destroys the whole idea of resetting to a valid value. Also I see no reason for validating the value again since timepicker has already done this once. However ixf you are not willing to fix this please tell me how to easily reuse the timepicker validation to check the manual inpu against the chosen format :)
@CasperWSchmidt, is the ticket #132 a follow up on this, or are they separate issues?
I believe they are seperate issues. My suggestion in this ticket might fix the other problem though because the timepicker should check for empty value and fire an error event :)
Hi Casper, the timepicker events have been reworked a bit to handle this. changeTime
will now only be fired when a valid time is selected or entered.
Hi Jon
Thanks a lot. Does this change mean that changeTime is now also fired when manually typing a valid time in the box?
Yes, it should be.
Using this code: // Create timepicker $('#StartTime').timepicker({ forceRoundTime: true, minTime: '<% =getMinTime() %>', maxTime: '<% =getMaxTime() %>', step: '<% =getDefaultTimeAccuracy() %>', timeFormat: '<% =strTimeFormat %>' }); $('#StartTime').bind('timeFormatError timeRangeError', function() { alert('<% =Replace(Replace(JT("Please choose a valid time between %1 and %2"), "%1", getMinTime()), "%2", getMaxTime()) %>'); $('#StartTime').focus(); });
If i try entering something not valid in the input field I get the alertbox (OK!). However, if I then select a valid time from the dropdown I get the alertbox again and the value in StartTime is not updated!
I then tried a workaround this way: // Create timepicker $('#StartTime').timepicker({ forceRoundTime: true, minTime: '<% =getMinTime() %>', maxTime: '<% =getMaxTime() %>', step: '<% =getDefaultTimeAccuracy() %>', timeFormat: '<% =strTimeFormat %>' }); $('#StartTime').bind('timeFormatError timeRangeError', function() { alert('<% =Replace(Replace(JT("Please choose a valid time between %1 and %2"), "%1", getMinTime()), "%2", getMaxTime()) %>'); $('#StartTime').val($('#StartTimeHidden').val()); $('#StartTime').focus(); }); $('#StartTime').bind('changeTime', function() { $('#StartTimeHidden').val($('#StartTime').val()); });
Which works great, except when you enter a valid time manually, loose focus and then enter something not valid. Then the value is updated to the newest time value chosen with the timepicker. Perhaps changeTime event should fire on manual input as timeFormatError and timeRangeError seems to do just fine?