Problem
Using field #type = 'image_button' may cause some "errors" when I want to check which button was pressed - by checking $form_state['clicked_button']['#value'] in _validate or _submit function.
For example in form like:
$form['normal_sub_1'] = array(
'#type' => 'submit',
'#value' => 'normal_sub_1',
);
$form['image_sub_1'] = array(
'#type' => 'image_button',
'#value' => 'image_sub_1',
);
$form_state['clicked_button']['#value'] is always image_sub_1 no matter whether I click normal_sub_1 or image_sub_1
but in
$form['image_sub_1'] = array(
'#type' => 'image_button',
'#value' => 'image_sub_1',
);
$form['normal_sub_1'] = array(
'#type' => 'submit',
'#value' => 'normal_sub_1',
);
everything is all right.
As You can see buttons order is problem in this case.
If You want to reproduce this problem, install attached module change fields order.
| Comment | File | Size | Author |
|---|---|---|---|
| #3 | buttons.zip | 1.25 KB | marcin maruszewski |
| Simple module with form | 871 bytes | marcin maruszewski |
Comments
Comment #1
star-szrThanks for the report, Marcin!
$form_state['clicked_button'] has been deprecated in favor of $form_state['triggering_element']. triggering_element should work better.
See this issue:
#1049462: Usage of deprecated $form_state['clicked_button'] causes bugs during AJAX submissions by non-buttons
And this link from the upgrade guide:
http://drupal.org/update/modules/6/7#clicked_button
Comment #2
star-szrActually, I can't reproduce this even with the provided test module.
Comment #3
marcin maruszewski commentedCottser, thanks for support.
Unfortunately, when I have two image_buttons or image_button is last button in $form, $form_state['triggering_element'] returns wrong value.
Try out attached module.
Comment #4
David_Rothstein commentedThanks for the great test module!
This is reproducible, but is a duplicate of a couple other issues:
#873070: When an image button appears after another button in a form, the wrong triggering element and #submit handlers are detected
#1452894: Elements with #has_garbage_value (image buttons) are always set as a triggering element
Both issues have patches in progress also.
(I mentioned in a comment on the first one that the code in the example module here could be used to help write automated tests, though.)
In the meantime, my understanding is that a workaround is to simply set the #name property on the image button to something unique (and leave the #value property completely unset).... Switching from #name to #value [edit: I meant "from #value to #name"] in that way should solve the problem.
Comment #5
David_Rothstein commented(Well, technically it's a duplicate of the first more than the second, since the second is newer than this one :)
Comment #6
marcin maruszewski commentedDavid, thanks for Your reply, workaround solution and information about other issues.
Comment #6.0
David_Rothstein commentedmistake in description