0

I have a form in a custom Drupal 6 module built with the Form API, it has 1 or more image_button elements in a list followed by the Save and Cancel buttons.

Everything is working fine when clicking the image and standard buttons, they call the submit functions they should, but if I hit the [ENTER] key within any of the text fields in the form the first button in the form is submitted, which unfortunately in this case is an image_button in the list rather than the Save button.

This is a standard problem with web forms, you generally have to hack in a hidden (by style and/or size) button early in the form definition to ensure the default submit path is called (in this case that's what the Save button will call) rather than the submit path for the buttons that are before the button you want to be the default.

Is there some Drupal 6 magic that enables setting a default button regardless of where it is in the form definition that I've failed to find in the docs, or should I create a phantom submit button that is styled to not be visible?

Thanks in advance for any answers.

ianmjones
  • 3,395
  • 1
  • 25
  • 26

3 Answers3

0

Try to put '#weight' to your buttons.

Vlad Stratulat
  • 1,296
  • 1
  • 10
  • 24
  • I tried using '#weight' in various ways, but all it does is effect where in the form the element ends up, which will not get around the problem of the first button being the default when pressing [ENTER]. – ianmjones Nov 22 '11 at 15:08
0

The answer seams to be no, there is no built in Drupal method of getting around the problem of the first button in a form being the default when pressing [ENTER].

Here is what I had to do.

I created an extra submit button at the top of my form:

// A submit button that needs to be hidden, it'll be the default rather than one from the list.
$form['hidden-submit'] = array(
  '#type' => 'submit',
  '#value' => 'Save',
  '#attributes' => array('class' => 'hidden-default-submit'),
);

In the CSS included for the page I added the following belts and braces CSS to hide the extra submit button in as many web browsers as I could test:

.hidden-default-submit {
  position: absolute;
  left: -2000;
  top: -2000;
  width: 0;
  height: 0;
  border: 0;
  margin: 0;
  padding: 0;
}
ianmjones
  • 3,395
  • 1
  • 25
  • 26
-1

you can easily do this by assigning different names to each submit button