0

I'm developing a pretty basic module catalog_request that creates a form. I'm replacing the form's default submit button <input ..type="submit" /> with a <button ...>click</button> using following in form definition.

function catalog_request_form($form, &$form_state) {

    /**
     * form fields here
     */

    $form['fieldset_submit']['the_submit_button'] = array(
       '#type' => 'markup',
       '#prefix' => '<button class="btn submit">',
       '#markup' => 'Send Request...',
       '#suffix' => '</button>',
    );

    /* ... */
}

and have defined other two form related functions

function catalog_request_form_validate($form, &$form_state) {
    drupal_set_message(t('The form was validated'));
}

function catalog_request_form_submit($form, &$form_state) {
    die('submitted');
    //drupal_set_message(t('The form was submitted'));
}

The problem

My submit function catalog_request_form_submit isn't being called. I've read numerous articles online but none resolved my issue. Following didn't work

  1. Adding '#submit' => 'catalog_request_form_submit', to the_submit_button key
  2. Adding '$form['#submit'] => 'catalog_request_form_submit'; to end of catalog_request_form function
  3. Adding '$form['#submit'][] => 'catalog_request_form_submit'; to end of catalog_request_form function

I've read that adding something along the lines of $form['actions']['#submit'][] = 'custom_function'; could solve the problem but I'm not sure how to adapt it for my form.

I know that having '#type' => 'submit' for the submit button makes the form work but I want a button tag.

My problem is exactly When altering the submit button, the form does not work but it doesn't have a solution (only an answer that ignores the question's point)

P.S. I do not develop for drupal frequently so I might be missing something very obvious.

Any help will be greatly appreciated. thanks

4
  • 1
    Instead of adding a button you can use another custom submit button? And why you need a button?
    – Inizio
    Commented Sep 12, 2014 at 5:06
  • Also buttons will not submit a form. Try to use another form submit button. Refer Handle multiple submit buttons in Drupal form API
    – Inizio
    Commented Sep 12, 2014 at 5:22
  • The real question is why you're stuck on using a <button> where it doesn't make sense to use one. If you want something that submits the form, use a submit input, which was created for that specific purpose.
    – Clive
    Commented Sep 12, 2014 at 9:05
  • I know that <input ... type="submit"../> submits the form. I know that using '#type'=>'submit' generates an input element. But I have to use <button...>. why? I'll start with "button can have HTML content". The <input ... type="submit"../> and <button..> both submit the form in all browsers I care about so there is no issue there. There are ways around every issue and I'm willing to get around this issue when somebody proves that there is no way this problem can be resolved in Durpal. Drupal is the problem here, not my HTML. thanks for your help guys :)
    – Ejaz
    Commented Sep 12, 2014 at 10:00

4 Answers 4

2

In order to add another submit handler you have to add something to hook_form_alter(): check for your particular form ID, and then add a submit handler in one of the following ways:

OPTION 1:

To add a submit handler that is called as the next callback after processing the default submit handler callback you can use this:

function mymodule_form_alter(&$form, &$form_state) {
  if ($form_id == 'form-id-of-your-form') {
    $form['actions']['submit']['#submit'][] = 'mymodule_whatever_function';
  }
}

OPTION 2:

If OPTION 1 is not working for you, replace the third line with this:

form['#submit'][1] = 'mymodule_whatever_function';

This will add a second submit handler, or replace the second one with yours if there is already more than one.

OPTION 3:

To remove the default submit handler completely (so that "nothing will happen" on submit, or so that you can replace it with your own), use code like this:

function mymodule_form_alter(&$form, &$form_state) {
  if ($form_id == 'form-id-of-your-form') {
    unset($form["actions"]['submit']);
  }
}
0

From the link you provided:

"In order for the form to be submitted correctly the element needs to be a submit, button or image_button type."

So maybe something like the following will work.

$form['fieldset_submit']['the_submit_button'] = array(
    '#type' => 'button',
    '#name' => 'name',
    '#value' => t('Submit'),
    '#executes_submit_callback' => TRUE,
);
1
  • Unfortunately '#type' => 'button' generates an input element, not a button element Commented Sep 12, 2014 at 5:01
0

https://api.drupal.org/api/drupal/includes!form.inc/function/theme_button/7

You can write something like this in your theme

function THEMENAME_button($variables) {
  $element = $variables['element'];
  $element['#attributes']['type'] = 'submit';
  element_set_attributes($element, array('id', 'name', 'value'));

  $element['#attributes']['class'][] = 'form-' . $element['#button_type'];
  if (!empty($element['#attributes']['disabled'])) {
    $element['#attributes']['class'][] = 'form-button-disabled';
  }

  return '<input' . drupal_attributes($element['#attributes']) . ' />';
}

and change markup for you needs.

Internet Explorer 6-7 doesn't work with button value attribute.

Update

function THEMENAME_button($variables) {
  $element = $variables['element'];
  $element['#attributes']['type'] = 'submit';
  element_set_attributes($element, array('id', 'name', 'value'));

  $element['#attributes']['class'][] = 'form-' . $element['#button_type'];
  if (!empty($element['#attributes']['disabled'])) {
    $element['#attributes']['class'][] = 'form-button-disabled';
  }

  return '<button' . drupal_attributes($element['#attributes']) . '>'. $element['#attributes']['value'] .'</button>';
}
2
  • it seems to output an <input...> element not <button...>
    – Ejaz
    Commented Sep 12, 2014 at 10:43
  • It's standard theme_button function. You can change it in your theme. Commented Sep 12, 2014 at 11:33
-3

If you are rendering your form using form--anyformname.tpl.php template file then make sure in that template file you are rendering these three thing at the end:

    <div>        
    <?php print render($form['form_id']); ?>
    <?php print render($form['form_build_id']);?>
    <?php print render($form['form_token']); ?>
    </div>
2
  • The OP is not using a theme, but a module.
    – avpaderno
    Commented Jul 30, 2015 at 11:56
  • In that module did u created a filename.tpl.php file, if created then make sure all hidden child element should in form when it get render.
    – Manish
    Commented Oct 2, 2015 at 9:43

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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