Problem/Motivation
When creating a form via form API, ajax API can be used to upate partial form parts using #ajax command. Date element #type does not support that.
Ex.:
$form['date'] = [
'#type' => 'date',
'#default_value' => date('Y-m-d'),
'#ajax' => [
'callback' => '::dateCallback',
'wrapper' => 'table-wrapper',
],
];
Reproduction scenario
In order to easily test (as a human) that the patch work. Here is a testing module.
Steps:
- Install given module
- Move to move to /ajax_date page : when you change the date (via keyboard, or via picker), nothing special happens. (useless to submit the form, I did not implement submit method
- Apply the given patch
- Clean cache and make sure the /ajax_date page is not served from cache.
- Now change the date : ajax throbber appears and a line is added beneath the date field: 'Selected date: xx'

Proposed resolution
Allow support of #ajax for Date render element.
API changes
Date FAPI element will now support #ajax.
No break is forcasted for other elements, such as date without #ajax.

Comments
Comment #2
dom. commentedHere is a patch attached. The syntax given in issue description is now allowed, and the testing module given refreshes the page via AJAX.
Comment #3
dom. commenteddamn.. forgot "need review" !
Comment #4
dom. commentedComment #5
anavarreTested against 8.3.x and couldn't get the date selection message to show up via AJAX.
Comment #6
dom. commentedAlso tested on 8.3.x and confirmed working there too.
Still I would suggest to add this on 8.2.x since it is a simple patch, but a nice to have feature for #ajax and form API consistancy.
Also updated the issue summary to add reproduction steps: @anavarre: did you clear cache after applying patch ? I guess the form rendering get cached.
Comment #7
dom. commentedComment #8
anavarreNot sure what happened. Reinstalled 8.3.x, reinstalled the module and applied the patch and now it's working well.
To get this to 8.2.x I think you'll need to get a beta target from a release manager.
Comment #9
dawehnerNote: This could still land in 8.2.1 for example
Comment #10
dom. commentedAdd a test-only patch (should fail) and a patch combining the test only and previous patch.
The test defines a date field on a form and test that the callback runs on change.
Comment #13
dom. commentedUpdate test to fix error.
Comment #15
dom. commentedreroll
Comment #16
dom. commentedComment #17
dom. commentedChange the issue to Bug since using #ajax on this FAPI element is an announced feature.
Raise this bug to Major because :
Here is a feature example of what cannot be done without this AJAX enabled date field:

Without this issue fixed, you cannot ajax refresh the table when date changes.
Comment #18
dom. commentedChange to forms system, since this is more a FAPI then AJAX issue itself.
Comment #19
dawehnerIts sooooo great to see javascript tests written by people!
The tests should live in
core/tests/Drupal/FunctionalJavascriptTests/Ajaxto be picked up by the testbotSmall suggestion: use a maybe slightly better variable name like $callback_object or so
Comment #20
dawehnerTo be fair it is a bit of a blocker, when we write tests and they aren't actually executed :)
Comment #21
dom. commentedUpdate test as per @dawehner comments:
- move test to new location
- changed variable name
Comment #22
mpdonadioI was looking at this and thinking the same thing, but oddly enough it is showing up in the logs as being run:
Comment #24
dom. commentedI just moved the test on #21, nothing else. What means the error "FATAL Drupal\FunctionalJavascriptTests\Ajax\AjaxCallbackTest: test runner returned a non-zero error code (255)." ?
I can't reproduce locally this...
Comment #25
dom. commentedSame patch as per #21 just for reroll.
Comment #27
dom. commentedOK... I did forgot to change the namespace after changing the test directory... Don't laugh at me... please.. !
Should be better this time.
Comment #29
dom. commentedComment #30
lendudeNice, more javascript tests!
Bit of nitpicking:
I think we use full URLs for this, not just issue numbers.
Wouldn't it make more sense to not have a date by default? Since that is what the message says.
Unused Use statements
Tests AJAX form elements or Tests an AJAX form element, one of the two
Comment #31
dom. commented@Lendude: thansk a lot for review !
1. oh.. okay ! Hopefully the change is correct now
2. totally makes sense
3. bad IDE: you should have told me ! bad IDE...
4. indeed, and because of this, to make sense with the test form of (1), I also added the @todo here to make it match.
New patch attached with corrections.
Comment #32
mpdonadioDid a quick look at this; don't have time to manually test this.
For the long term, I think calling this testDateCallbacks() would be better in the long term so that additional elements can have their own test methods in that class (instead of lumping them all here). Also would adjust the docblock on the method accordingly.
Nobody is around on IRC right now to verify, I don't think we can commit a patch with a @todo w/o a reference to the followup issue that takes care of the todo. Also elsewhere in the patch.
Two things here. Calling date() is unnecessary; a hardcoded value should be used to make this predictable. Also, negative assertions should be followed by positive ones. So, that responseNotContains() should be followed by asserting that the response contains something expected.
Setting NW mainly for this bit.
Comment #33
dom. commented@mpdonadio: Here is a new patch following your review. Here are some comments regarding it:
1- OK. But I rather called the method testDateAjaxCallback(). Also consequently, I changed class name (thus file name) to: AjaxCallbacksTest with an s. Also changed it's docblock thus to
* Tests AJAX callbacks on FAPI elements.Method docblock changed to :
* Tests if ajax callback works on date element.2- Remove mention of follow-up. Just added a simple todo. After all, that's all what's needed !
3- Remove the date() as suggested and hardcoded an arbitrary date. Also added a positive responseContains() to double check the correct filled date has been added.
NOTE: also changed mention of AJAX to Ajax for consistency.
Comment #34
dom. commentedUp for review
Comment #35
dom. commentedUpdate Issue tags
Comment #36
dom. commentedUp version, try re-upload #33 to see if applying to 8.3.x. No changes.
Comment #37
xjmNote that you can add tests against a particular branch using the "Test with" dropdown when you upload a file or the "Add test" link under the patch. Thanks!
Comment #39
martijn houtman commentedNote: this seems to work for field type 'date', but not yet for type 'datetime'.
Comment #40
dom. commentedCan we focus on reviewing patch here for 'date' and work on datetime in the appropriate issue ?
#2783615: Datetime FAPI element type do not allow AJAX via #ajax API
Comment #41
tanmaykPatch in #36 worked for me. +1 for RTBC.
Comment #43
mpdonadio#36 doesn't apply, so it needs to be rerolled.
Nits, needs to be updated to [] syntax.
We can remove these; we try to avoid @todo w/o f/up issues to point to.
I think this looks OK, but don't think I know enough about the Ajax subsystem to RTBC this once rerolled.
Comment #44
gaurav.kapoor commentedComment #45
gaurav.kapoor commentedReroll and fixed 43.1 and 43.2.
Comment #46
jofitzPlease remember to remove Needs Reroll tag.
Comment #48
k4v commentedWorks for me!
Also noticed it does not work with datetime, but this should be adressed in https://www.drupal.org/node/2783615. I briefly tried to modify the patch to also work with datetime, but that seems to require more work.
Comment #49
k4v commentedThe changes to the date logic already are commited to 8.5.x with https://www.drupal.org/node/2783615 now.
All thats left to do here is to add the test for the single date element.
Comment #50
k4v commentedI cannot do an interdiff, because the old patch does not apply anymore...
Comment #54
catchCommitted/pushed to 8.5.x and cherry-picked to 8.4.x. Thanks!