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.

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;
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.