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.

Comments

star-szr’s picture

Status: Active » Closed (won't fix)

Thanks 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

star-szr’s picture

Status: Closed (won't fix) » Closed (cannot reproduce)

Actually, I can't reproduce this even with the provided test module.

marcin maruszewski’s picture

Version: 7.12 » 7.20
Priority: Normal » Major
Status: Closed (cannot reproduce) » Active
StatusFileSize
new1.25 KB

Cottser, 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.

David_Rothstein’s picture

Status: Active » Closed (duplicate)

Thanks 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.

David_Rothstein’s picture

(Well, technically it's a duplicate of the first more than the second, since the second is newer than this one :)

marcin maruszewski’s picture

David, thanks for Your reply, workaround solution and information about other issues.

David_Rothstein’s picture

Issue summary: View changes

mistake in description