Can I make the second or later button in a Drupal form the default button?

人走茶凉 提交于 2019-12-25 03:54:50

问题


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.


回答1:


Try to put '#weight' to your buttons.




回答2:


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



回答3:


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



来源:https://stackoverflow.com/questions/8154912/can-i-make-the-second-or-later-button-in-a-drupal-form-the-default-button

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!