Early Bird Registration for DrupalCon Portland 2024 is open! Register by 23:59 PST on 31 March 2024, to get $100 off your ticket.
I wanted to be able to prevent users from selecting a date in the past from the datepicker popup. After some research into the jQuery datepicker plugin I discovered that you can pass the option "minDate" at the time the popup is called. I tested this by hard-coding an addition to the $settings array in date_popup.module. But it would be better if we added a selector to the date_popup element to allow any additional options to be passed through. I suggest:
date_popup.module line 277:
if (is_array($element['#datepicker_options']) && !empty($element['#datepicker_options'])){
$settings += $element['#datepicker_options'];
}
It could then be invoked when you define a form, for example:
'#datepicker_options' => array('minDate' => 'today'),
What do you think?
Comment | File | Size | Author |
---|---|---|---|
#28 | date-beforeShowDay-eval-1143680-28.patch | 630 bytes | solideogloria |
| |||
#23 | date-allow_to_use_beforeShowDay-1143680-23.patch | 885 bytes | marie.pinet |
| |||
#17 | date-6.x-2.x-dev.-datepicker-options.patch | 3.2 KB | MasteRRR |
#12 | date-popup-date-picker-mindate.jpg | 93.25 KB | alexweber |
#7 | date-allow-datepicker-options-1143680-7.patch | 2.84 KB | kristiaanvandeneynde |
Comments
Comment #1
Hubert_r CreditAttribution: Hubert_r commented+1 Great idea!
Comment #2
mmilo CreditAttribution: mmilo commentedThis would be -extremely- helpful if we want to customize the datepicker.
A good point of reference would be #625264: Improvements to date_popup module.
Comment #3
maykbrito CreditAttribution: maykbrito commentedTks bro! works great!
=)
Comment #4
maykbrito CreditAttribution: maykbrito commentedHow about 'onSelect' option?? My js was like this:
Comment #5
just_like_good_vibeshello and thanks for the tip !
just to be sure, you meant to patch the file "date_popup.module",
in function "date_popup_process_date_part" and just after the creation of the settings array, right?
i also changed a little bit the added code to :
cheers
Comment #6
kristiaanvandeneyndeEdit:
Never mind, there's a mixup in this one.
Look at the next comment...
Comment #7
kristiaanvandeneyndeAdded a patch that:
Please test and review.
Comment #8
kristiaanvandeneyndeSorry for all the mix-ups, I had some user unfriendly logic in setting the defaults.
This should work even better.
Comment #9
KarenS CreditAttribution: KarenS commentedCommitted, thanks!
http://drupalcode.org/project/date.git/commit/ade4a42
Let's backport this approach to D6. Anyone want to roll a patch for D6?
Comment #10
jim_at_miramontes CreditAttribution: jim_at_miramontes commentedHmm -- I'm running v. 7.x-2.6 of date_popup, but am not able to get it to recognize the datepicker options. My form element looks like this:
The datepicker comes up and works fine, but without the minDate restriction. Does anyone have any advice? Thanks!
Comment #11
kristiaanvandeneyndehttp://api.jqueryui.com/datepicker/#option-minDate
Is 0 even a valid setting?
Try setting it to something else and see if that works.
Comment #12
alexweber CreditAttribution: alexweber commented@kristiaanvandeneynde, 0 is actually a valid setting, as per this Stack Overflow answer as well as my own experience.
@jim_at_miramontes, I'm using jQuery UI Datepicker 1.8.11 (via jQuery Update) and Date 7.x-2.6 and can confirm 100% that it does indeed work. I incidentally just implemented this for a project, see attached screenshots from my code and website...
Are you using an older jQuery UI version?
Comment #13
MasteRRR CreditAttribution: MasteRRR commentedHello, everybody. How can I use datepicker options which needs a functions as arguments?
example: http://api.jqueryui.com/datepicker/#option-beforeShowDay
I tried to disable weekends and holidays using this solution, but can't call javascript function.
Is it possible? What should I do to solve this problem?
Comment #14
kristiaanvandeneyndeYou can't pass functions to JavaScript from PHP because they would be passed as strings instead of reference types.
A 'very bad idea ™' workaround would be to adjust the JS that creates the datepicker and eval() the #datepicker_options keys that expect a callback.
Comment #15
jonhattan@MasteRRR I managed to solve this and posted an answer on stackexchange: http://drupal.stackexchange.com/questions/58968/customize-days-that-can-...
Comment #16
kristiaanvandeneyndeJonhattan's idea should work.
P.S.: I'm glad to see people are actually finding the #datepicker_options key useful :)
Comment #17
MasteRRR CreditAttribution: MasteRRR commentedThanks @jonhattan and @Kristiaanvandeneynde, your help was very useful.
Jonhattan's idea works greatly and I applyed this approach for Drupal 6.
Also I've backported Kristiaans patch for Drupal 6, so if testing show no errors, this issue can be closed.
Comment #18
jim_at_miramontes CreditAttribution: jim_at_miramontes commented@alexweber It's working for me; I was outsmarting myself with some ajax games.
Comment #19
nightlife2008 CreditAttribution: nightlife2008 commentedI solved this problem using the HOOK_date_popup_process_alter(), which is run AFTER the element has been processed, to add the #datepicker_options array.
After adding the options, I just reprocess the element with the proper options set.
Works like a charm!
Comment #20
akalata CreditAttribution: akalata commentedThanks for the example, nightlife2008!
Comment #21
jimmyko CreditAttribution: jimmyko commentedIs it fixed? I am finding help on applying data_popup options with jQuery function. I find something help from the comment above. But since this was already implemented the versions after 7.x-2.6. I don't know how is the progress on 6.x
If anyone confirm it is fixed. I do think this issue should be changed to closed.
Comment #22
zhiqiang.qiu CreditAttribution: zhiqiang.qiu commentedit looks great!
Comment #23
marie.pinet CreditAttribution: marie.pinet commentedHello,
Here is a patch that allow the use of the beforeShowDay feature of datepicker.
It is for the 7.x-2.x version.
Example:
Where EnableOnlyFriday is a JS function in my custom code. The problem I had was that datepicker API require a function name and not a string of the function name.
Comment #24
DamienMcKennaSwitching this to the D7 branch.
Comment #25
solideogloria CreditAttribution: solideogloria commentedPatch #23 has been working for a several months for me, though it only addresses a single property, but it was the one I needed.
Comment #26
solideogloria CreditAttribution: solideogloria commentedComment #27
DamienMcKennaThe patch needs to be updated to match Drupal's coding standards (indenting), and a comment would be useful.
Comment #28
solideogloria CreditAttribution: solideogloria commentedI tried the solution linked in #15, and it works:
I think I'm willing to follow the safer path of not adding an eval statement. However, if you think otherwise, I modified the patch as stated in the previous comment.
Comment #29
solideogloria CreditAttribution: solideogloria commentedI also noticed that the if block is before the switch statement. Would it be better to have it only if
case 'datepicker'
?Comment #30
AnybodyRe @DamienMcKenna
This has been done in #28, while I'm not feeling very well about the eval(). See https://www.digitalocean.com/community/tutorials/js-eval
Is there a better pattern to allow defining the function server-side and pass it to the jQuery UI element?
Otherwise I think the JS code in #28 is the better alternative.
Comment #31
solideogloria CreditAttribution: solideogloria commentedNote: I still think #15 is the best way to go (see this SE answer).
However, I just did some work with a "better eval" recently. Though you cannot define a function server-side and execute it client-side without evaluating it (because it has to be executed somehow, like with eval), there are safer ways to execute it.
Never use eval()!
I ended up using something like this for my purposes:
So for an improvement to #28, I think we could use this line instead of the line with eval():
If someone wants to consider updating the patch and testing it, they can do so.