2

a possibly stupid question. Why won't this form submit?

$form['foo'] = array(
    '#type' => 'textfield',
);
$form['submit'] = array(
    '#type' => 'submit',
    '#value' => 'Submit',
);

if I add the following

$form['#submit'][] = 'my_form_submit';

and a submit function:

function my_form_submit($form, &$form_state){
  dd('Submit ran');
}

The debug statement is never called. What am I doing wrong?

  • I assume there is not a my_form_validate function that is not passing? – Ashlar Jan 10 '12 at 0:49
4

Actually the submit handler name should be the form function name with _submit.

So in this case: my_form() returns the form, and my_form_submit($form, &$form_state) is automatically the submit handler. This also works for _validate.

See the Examples project for some working code. It has a submodule called form_example.

| improve this answer | |
3

The code you have pasted looks fine, so why doesn't it work? It's hard to say exactly, but some possibilities are:

  • The submit handler is called but you are tracking the wrong file or there is an error with the dd function.
  • The form is not loaded on the page that is rendered on display.
  • The submit handler is in a include file not loaded when the form is processed.
  • Typos in the actual code (mis name of function names).

This is some guesses, like I said what you have looks fine. One comment though. You don't have to add the submit handler if you name it FORM_NAME_submit, Drupal will by default add those to the functions called on submit.

| improve this answer | |
3

I think the standard way to do it is to put it in an 'actions' container and then add the '#submit' to that. try this snippet instead of your code above.

$form['actions'] = array('#type' => 'actions');
  $form['actions']['submit'] = array(
    '#type' => 'submit', 
    '#value' => t('Submit'), 
    '#weight' => 5, 
    '#submit' => array('my_form_submit'),
  );

notice how the button gets the #submit... not the form. Forms do have a default submit handler, which I think is myformname_submit but I always do it explicitly anyway. Also, I've seen DD fail on occasion due to random permissions problems. Maybe try DPM, watchdog, KPM or something that writes to the screen rather than a log file.

| improve this answer | |
  • Hmm.. I changed the form to this. pastebin.com/hR2L3dX2 It still doesn't submit.. :/ – starsinmypockets Jan 9 '12 at 22:52
  • Adding the submit button to the actions container doesn't influence if the #submit is called or not. Also be aware that adding the submit handler to the button alters the functionality (in case more submit buttons are added later on) – googletorp Jan 10 '12 at 11:18
  • Right, so in the case of multiple buttons this puts the submit handlers with the buttons. I like to do it that way so I can easily see each button's submit. @starsinmypockets there's no reason that shouldn't run, your code looks fine to me. Can you try using dpm($form) in your handler? Is there any other code you're not showing us? Maybe there is more to $form we're not seeing? Sometimes accidentally override stuff in PHP arrays. – Jonathan Rowny Jan 10 '12 at 15:45
0

The problem here isn't the array, but the way that I invoke the form. I just needed to call my form callback using drupal_get_form('my_form_callback'); Thanks to all for the help!

| improve this answer | |
0

I had a lot of issues getting this working. I tried all suggestions in here, including swapping from drupal_build_form to drupal_get_form, using $form['submit']['#submit'] vs $form['#submit'], and even just using a link that linked to current_path() as a #markup element. All of these attempts failed.

What did work was the following:

  $reset_button_text = t('Reset filters');
  // Before building the form check the query parameters for the reset op and use a goto to stop the form and "reset" it.
  $query_params = drupal_get_query_parameters();
  if (isset($query_params['op']) && $query_params['op'] == $reset_button_text) {
    drupal_goto(current_path());
  }

  // Form declaration goes here..
  // ....
  // ....
  $form['#method'] = 'get';
  // Reset button just resets the filters.
  $form['actions'] = array('#type' => 'actions');
  $form['actions']['reset'] =  array('#type' => 'submit', '#value' => $reset_button_text);
  $form['actions']['submit'] = array('#type' => 'submit', '#value' => t('Apply filters'));

This seems like a massive hack and only works because I have the form as a sort of filter form for a custom EntityFieldQuery (which outputs some other markup) on the same page. However, it works a charm for me :)

| improve this answer | |

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.