0

I have a submission handler for my custom form that doesn't work. I tried many solutions, but nothing works.

My_Custom.module

 [...]
function tournament_menu() {
    $items['tournament/newteam'] = array(
        'title' => 'Créer une nouvelle équipe',
        'page callback' => 'drupal_get_form',
        'page arguments' => array('form_newteam'),
        'access callback' => TRUE,
        'file' => 'form_tournament.inc',
    );    
}
 [...]

form_tournament.inc

function form_newteam ($form, &$form_state) {

 [...]
    $form['submit'] = array(
        '#type' => 'submit',
        '#value' => 'Créer',
    );
    $form['actions']['submit']['#submit'][] = 'form_newteam_submit';

    return $form;
}

function form_newteam_validate($form, &$form_state) {
    //Validation logic
}

function form_newteam_submit($form, &$form_state) {

    drupal_set_message("Bla bla submit !");

}    

My validation function works.

I try all this code In form_newteam() function.

$form['actions']['submit']['#submit'][] = 'form_newteam_submit';
$form['#submit'][] = 'form_newteam_submit';
$form['#submit'] = array('form_newteam_submit');
$form['submit'] = array(
 '#type' => 'submit',
 '#value' => 'Créer',
 '#submit' => array('form_newteam_submit'),
); 


$form['submit'] = array(
  '#type' => 'submit',
  '#value' => 'Créer',
  '#submit' => 'form_newteam_submit',
);
  • There is many form_newteam_submit reference in your code. I hope that you are trying them one by one, not all together. – Jayendra Kainthola Nov 11 '13 at 17:38
  • Have you cleared cache? – Potney Switters May 18 '15 at 13:33
1

Go back and scrutinize your validation function. The code you are showing here actually works, so the problem is something you did not include in your sample.

For the reference, the working implementation over here is:

function form_newteam ($form, &$form_state) {
    $form['submit'] = array(
        '#type' => 'submit',
        '#value' => 'Créer',
    );

    return $form;
}

function form_newteam_validate($form, &$form_state) {
    drupal_set_message("valid");
}

function form_newteam_submit($form, &$form_state) {
    drupal_set_message("submit");
}

Please note that your function names do not conform to the drupal coding standards. Function names must be prefixed with the module name. Therefore you should name your functions like this: tournament_newteam or tournament_newteam_form. If those are administrative forms, stick them into tournament.admin.inc otherwise place them into tournament.pages.inc.

  • I try to set the minimal code but, drupal_set_message() don't work in my valid and submit function now. – Skalv Nov 12 '13 at 11:35
  • I recommend to start over and install drupal again, then build up your custom module step by step in the clean environment. I actually tested the code I submitted. The form API is supposed to work like this. – znerol Nov 12 '13 at 14:19
  • Yep, I try with a clean install of drupal and my module work correctly. thank ! – Skalv Nov 12 '13 at 14:40
-1

try

function form_newteam_submit(&$form, &$form_state, $form_id = 'form_id') {

    drupal_set_message(t('Bla bla submit !'), 'status');

} 
  • You've copied the function signature for hook_form_alter() there - that's not related to the signature of the submit function, two separate things altogther – Clive Nov 11 '13 at 16:35
  • Clive, that is incorrect. I have copied this from my own module and it does work. If the answer is incorrect to your knowledge please state that but you should not be making assumptions. – sylwester Nov 11 '13 at 16:40
  • 3
    I'm not making assumptions - if you've copied the above from your custom code, then it has originally been copied from the signature for hook_form_alter() or hook_form_FORM_ID_alter() (look them up, you'll see straight away). Either way, it's not correct for the signature of a submit handler. $form should not be passed by reference, and there's no third parameter. In your case it works by luck, not by design – Clive Nov 11 '13 at 16:47
  • 1
    @sly, you have set third argument as default arguments, so may be your code is working. But, clive is right, your code example is signature of hook_form_alter, not for hook_form_submit. – Jayendra Kainthola Nov 11 '13 at 17:35

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.