Button element doesn't allow anything but type="submit"

Ryan Palmer - July 30, 2008 - 21:28
Project:Drupal
Version:6.x-dev
Component:forms system
Category:bug report
Priority:normal
Assigned:Unassigned
Status:duplicate
Description

As per this very old forum discussion, creating form element buttons with a type of anything other than type="submit" is not possible as we can see it is written directly into the return value below:

From forms.inc:

<?php
/**
* Theme a form button.
*
* @ingroup themeable
*/
function theme_button($element) {
 
// Make sure not to overwrite classes.
 
if (isset($element['#attributes']['class'])) {
   
$element['#attributes']['class'] = 'form-'. $element['#button_type'] .' '. $element['#attributes']['class'];
  }
  else {
   
$element['#attributes']['class'] = 'form-'. $element['#button_type'];
  }

  return
'<input type="submit" '. (empty($element['#name']) ? '' : 'name="'. $element['#name'] .'" ') .'id="'. $element['#id'] .'" value="'. check_plain($element['#value']) .'" '. drupal_attributes($element['#attributes']) ." />\n";
}
?>

This code is Drupal 5.9 but also applies to Drupal 6.3.

This code contradicts the Forms API Reference, as well as published print materials as noted in the forum discussion: Forms API Ref: #button_type

This check for #button_type and change to the return value should fix things:

<?php
function theme_button($element) {
 
// Make sure not to overwrite classes.
 
if (isset($element['#attributes']['class'])) {
   
$element['#attributes']['class'] = 'form-'. $element['#button_type'] .' '. $element['#attributes']['class'];
  }
  else {
   
$element['#attributes']['class'] = 'form-'. $element['#button_type'];
  }

 
$element['#button_type'] = (isset($element['#button_type'])) ? $element['#button_type'] : 'submit';

  return
'<input type="'. $element['#button_type'] .'" '. (empty($element['#name']) ? '' : 'name="'. $element['#name'] .'" ')  .'id="'. $element['#id'].'" value="'. check_plain($element['#value']) .'" '. drupal_attributes($element['#attributes']) ." />\n";
}
?>

Does this make sense? If so, I'll roll patches against 5.9 and 6.3.

#1

Ryan Palmer - July 31, 2008 - 02:19
Title:Button form element doesn't allow for anything but type="submit"» Button element doesn't allow anything but type="submit"

Shorter title... hopefully more eyeballs.

#2

dman - July 31, 2008 - 03:00

Is this the problem you are addressing?
http://drupal.org/node/72855

There is a discrepancy between what Drupal API thinks is a form '#type'=>"button" element and what HTML thinks is a form type="button" element. :(

If you create a '#type'=>"button" you actually get <input type="submit"> which sends the form back to the server but the Drupal FAPI doesn't actually run the submit code :-}
You do not get <input type="submit"> in any way. This is a huge terminology disconnect.

There still is, AFAIK, no supported way to add a proper HTML 'button' or 'reset' element other than by having it into #markup

The PROBLEM however is that it's now basically impossible to switch back to making Drupal-button == HTML-button as all throughout the codebase 'buttons' are being used to submit (and preview and validate). See the discussion above. Unfortunately this means your patch will KILL a large part of current Drupal functionality!

The API ref could probably have better documentation explaining that even 'buttons' are submitted to the server.
And we could add a new type of 'button' to actually produce HTML buttons to disambiguate. ... Although it may have the opposit e effect.

I've learnt to live with the idea that actionless buttons are not a Drupal server-side job, and that 'reset' buttons are entirely useless. So since then I've not needed other button types. Except for AJAX, and this just forces me to keep my Javascript "unobtrusive"... although it's still awkward at times.

Ya see what I'm saying ? :-{

#3

Ryan Palmer - July 31, 2008 - 03:38
Status:active» duplicate

I see what you're saying, and my bad for not digging a bit deeper before posting this issue.

So should this issue be refocused as "Change all those damn submit buttons in Drupal to the way they were meant to be written"? :)

Marked as duplicate.. will pick up with the original issue.

#4

dman - July 31, 2008 - 09:59

The change ain't going to happen, as there IS a significant use for the send-data-to-server-but-don't-run-FAPI-submit drupal button. It's very widespread. And useful as it is.
HTML type=button buttons basically do absolutely nothing without javascript. And are used very rarely.

A clarification that (button != button) and work-around in the documentation is the most likely way forward.

.dan.

#5

David Stosik - September 1, 2008 - 12:16

And what about the reset button, which may be needed in some forms, but still impossible to create other than using #markup?

Thanks,
David

#6

dman - September 1, 2008 - 12:24

The reset button is an un-feature ... especially in the days of AJAX and multi-step forms.

 
 

Drupal is a registered trademark of Dries Buytaert.