If you create a form with an image_button that has a #submit() specified on the image button, it doesn't call that #submit.
The attached demo module shows this at work. It doesn't matter which button you click: Button 2's submit handler is always processed.
If the image buttons are changed to 'submit', it works correctly.
Attached: Simple demonstration module.
| Comment | File | Size | Author |
|---|---|---|---|
| #25 | drupal-image_button_submit-873070-22-success.png | 34.57 KB | andymartha |
| #22 | drupal-image_button_submit-873070-22.patch | 5.74 KB | stpaultim |
| #14 | image_button_submit-5481602-14.patch | 5.76 KB | cvangysel |
| #14 | image_button_submit-5481602-14-no-fix.patch | 4.23 KB | cvangysel |
| #12 | image_button_submit-5481602-12.patch | 5.84 KB | cvangysel |
Comments
Comment #1
richthegeek commentedReproduced.
Comment #2
richthegeek commentedConfirmed issue is with image-button (switching to submit types does not cause the error. Having both submit and image_button on the same form causes the final image_button's value to be used)
Comment #3
richthegeek commentedComment #4
richthegeek commentedIssue related to form.inc/_form_button_was_clicked/2070
This code is browser-dependant and not checked thoroughly enough.
Chrome sets $form_state['input'][$element['#name']] so that the first if returns TRUE. However the second element basically always returns true for image_button elements (so the last image_button is always the clicked element).
However, in Firefox the POST name=>value is *not* set, so the first if returns false for all image_button's. The issue with the second if also fails.
Comment #5
richthegeek commentedFixed in Chrome + Firefox.
Both add NAME_x and NAME_y to the POST data, so testing for their existence should work. Needs testing across multiple browsers (I'm on linux so have no IE) although I don't see any problems there.
Comment #6
richthegeek commentedComment #7
xjmMinor points on code style:
"e.g." should have periods here; also, should "should be set" be inside the parentheses? Edit: Or the sentence needs to be reworded somehow, anyway. It's a little confusing at present.
elseif(one word) is the preferred standard for Drupal. (See http://drupal.org/coding-standards.)Also, I think we can probably use the test module from the original post to create an automated test for this (or we should try to, at least).
Thanks @richthegeek!
Comment #8
richthegeek commentedTested and patched against d8, coding style fixed, comments updated to be not rubbish.
Comment #9
kgoel commentedReproduced the problem and confirmed that patch work in Chrome 17.0.963.79, Firefox 10.0.2 and Safari 5.1.
I am on Mac so no IE so someone needs to test it on IE.
Comment #10
xjmThanks @kgoel!
We should test IE6-9 since this is tagged for backport.
Also still needs an automated test. Thanks!
Comment #11
cvangysel commentedWriting a test for this.
Comment #12
cvangysel commentedMy first attempt at a core patch. Could anyone review this?
Thanks
Comment #13
yesct commentedPlease make these comment lines wrap at 80 chars. See http://drupal.org/coding-standards
Also, please make two patches, one with the fix and the test (which should pass the test), and one with just the test (which should fail the test).
I ran into some trouble completing the review, but this is a start.
Comment #14
cvangysel commentedFixed the comment line wrapping inconsistency.
I added two patches to this comment; one should fail (without fix) and the other one should pass (includes fix).
Comment #15
cvangysel commentedComment #17
cvangysel commentedMarking this "needs review" as the test failure was expected.
Comment #18
attiks commented@cvangysel if you upload the patch without the fix first and then the patch with the fix, the status should be fine.
Comment #19
yesct commentedlooks like tests were added, so removing needs test tag.
Comment #20
yesct commented#14: image_button_submit-5481602-14.patch queued for re-testing.
Comment #21
yesct commentedComment #22
stpaultim commentedI rerolled this patch. Hopefully, correctly. Thanks @disasm for your patience.
Comment #23
stpaultim commentedComment #24
yesct commentedremoving reroll tag
Comment #25
andymartha commentedReproduced the problem and confirmed that patch drupal-image_button_submit-873070-22.patch by stpaultim in #22 shows "clicked submit button 1" for the first button in Chrome, Firefox, Safari and Opera on Drupal 8.x-dev My windows computer is back at the office so can't test for IE.
Comment #26
xjmThanks @stpaultim and @andymartha!
Comment #27
David_Rothstein commentedThis issue has several duplicates (in one case, the issue is older) but the patch here seems to be furthest along so I'm tentatively closing the others instead.
However, the other issues also have some good information that is worth reading (and in some cases some good code examples that could be used for tests - it sounds like the tests should be extended a bit to also assert that $form_state['triggering_element'] was set correctly):
#206955: image_button: clicked_button value is wrong
#1438614: image_button type and $form_state['clicked_button']
#1452894: Elements with #has_garbage_value (image buttons) are always set as a triggering element
From reading the above issues, my understanding is that this isn't necessarily a bug (although certainly confusing) for reasons semi-explained in the code comments visible in the patch:
So the way to handle this in calling code is to just set a unique #name on the image button (and don't set a #value at all), and then in theory everything should work. But, removing that restriction as the patch here does sounds like a good idea.
Finally, looking over the patch:
I don't understand why the #has_garbage_value check is being removed and replaced with a hardcoded check for a particular form element type?
Comment #28
David_Rothstein commentedActually, looking a bit more closely at #1452894: Elements with #has_garbage_value (image buttons) are always set as a triggering element, I'm not sure if the patch here is further along or that one is, so I haven't closed that issue yet.
Also, clarifying the title a bit. You don't need two image buttons on the same form to reproduce this. (The example module posted in #1438614: image_button type and $form_state['clicked_button'] is particularly useful for seeing this, and would be a great basis for expanded/improved automated tests.)
Comment #29
damien tournoud commentedSame at the other issue:
#valueis *not* an allowed property of<input type="image">.I'm closing this one as there is no actual bug in here, but I agree this is confusing. There is a DX discussion going on in #1452894: Elements with #has_garbage_value (image buttons) are always set as a triggering element.