0

The title says it all. I've created a form, but the submit hook isn't triggered once the form is submitted.

    <?php

//Create raffle form
function raffle_create_form()
{

    //Basic raffle info
    $form['#method'] = 'post';
    $form['raf_raffle_name'] = array(
        '#type' => "textfield",
        "#prefix" => "<h5>Raffle information</h5><hr>",
        "#title" => "Raffle Name:",
        "#required" => TRUE
    );

    $form['raf_allowed_number'] = array(
        '#type' => "textfield",
        "#title" => "How many people can join this raffle?",
        "#required" => TRUE
    );

    $form['raf_start_now'] = array(
        "#type" => "checkbox",
        "#title" => "Start raffle now?"
    );

    $form['raf_end_date'] = array(
        "#type" => "date",
        "#title" => "When to close this raffle?",
        "#required" => TRUE
    );


    $form['raf_prize_type'] = array(
        '#type' => "select",
        "#options" => array('gift' => 'Steam Gift', 'hardware' => 'Gaming peripherals'),
        "#prefix" => "<h4>Prize information</h4><hr>",
        "#title" => "Prize type",
        "#required" => TRUE
    );

    $form['raf_prize_name'] = array(
        '#type' => "textfield",
        "#title" => "Prize name",
        "#required" => TRUE
    );

    $form['raf_prize_about'] = array(
        '#type' => "textarea",
        "#resizeable" => true,
        "#title" => "Prize description"
    );

    $form['raf_prize_thumb'] = array(
        '#type' => "file",
        "#title" => "Prize thumbnail"
    );

    $form['raf_prize_link'] = array(
        '#type' => 'textfield',
        '#title' => "Link to product"
    );

    $form['raf_raffle_create'] = array(
        '#type' => 'submit',
        '#value' => "Create Raffle",
    );

    $form['#submit'][] = 'raffle_create_form_submit';
    return $form;
}

function raffle_create_form_validate()
{
    //validation logic
}

function raffle_create_form_submit()
{
    print_r($_POST);
}

Isn't the submit hook supposed to fire automatically?

What i'm doing now is i've set an action to a particular URL, made the menu hook call drupal_form_submit and passed the raffle_create_form_submit as an argument.

    <?php

//Create raffle form
function raffle_create_form()
{

    //Basic raffle info

    $form['#action'] = url('/admin/config/people/raffle/create/post');
    $form['raf_raffle_name'] = array(
        '#type' => "textfield",
        "#prefix" => "<h5>Raffle information</h5><hr>",
        "#title" => "Raffle Name:",
        "#required" => TRUE
    );

    $form['raf_allowed_number'] = array(
        '#type' => "textfield",
        "#title" => "How many people can join this raffle?",
        "#required" => TRUE
    );

    $form['raf_start_now'] = array(
        "#type" => "checkbox",
        "#title" => "Start raffle now?"
    );

    $form['raf_end_date'] = array(
        "#type" => "date",
        "#title" => "When to close this raffle?",
        "#required" => TRUE
    );

    $form['raf_prize_type'] = array(
        '#type' => "select",
        "#options" => array('gift' => 'Steam Gift', 'hardware' => 'Gaming peripherals'),
        "#prefix" => "<h4>Prize information</h4><hr>",
        "#title" => "Prize type",
        "#required" => TRUE
    );

    $form['raf_prize_name'] = array(
        '#type' => "textfield",
        "#title" => "Prize name",
        "#required" => TRUE
    );

    $form['raf_prize_about'] = array(
        '#type' => "textarea",
        "#resizeable" => true,
        "#title" => "Prize description"
    );

    $form['raf_prize_thumb'] = array(
        '#type' => "file",
        "#title" => "Prize thumbnail"
    );

    $form['raf_prize_link'] = array(
        '#type' => 'textfield',
        '#title' => "Link to product"
    );

    $form['raf_raffle_create'] = array(
        '#type' => 'submit',
        '#value' => "Create Raffle",
    );

    $form['#submit'][] = 'raffle_create_form_submit';
    return $form;
}

function raffle_create_form_validate()
{
    //validation logic
}

function raffle_create_form_submit()
{
    print_r($_POST);
}



//Menu hook

function raffle_menu() { $items = array();

$items['admin/config/people/raffle'] = array(
    'title' => 'Raffle',
    'access callback' => 'user_access',
    'access arguments' => array('Manage Raffles'),
    'file' => 'raffle_admin.inc',
    'page callback' => 'drupal_get_form',
    'page arguments' => array('raffle_admin_page')
);

$items['admin/config/people/raffle/create'] = array(
    'title' => 'Create Raffle',
    'access callback' => 'user_access',
    'access arguments' => array('Manage Raffles'),
    'file' => 'raffle_admin.inc',
    'page callback' => 'drupal_get_form',
    'page arguments' => array('raffle_create_form')
);

$items['admin/config/people/raffle/create/post'] = array(
    'title' => 'Creating Raffle!',
    'access callback' => 'user_access',
    'access arguments' => array('Manage Raffles'),
    'file' => 'raffle_admin.inc',
    'page callback' => 'drupal_form_submit',
    'page arguments' => array('raffle_create_form_submit')
);
return $items;

}

Is this the way it should be done?

4
  • Instead of print_r() to check your submit handler is called, use watchdog(). Also, it's not that the way to redirect users after they submitted a form.
    – avpaderno
    Commented Jan 15, 2016 at 7:58
  • I removed the action attribute and menu hook for the redirect, and tried watchdog(); and my hook is being fired. Thanks. Please post it as an answer so i can accept it
    – Haider Ali
    Commented Jan 15, 2016 at 8:03
  • Why get values from $_POST ? :S _form_submit should have a $form and a &$form_state arguments right? Commented Jan 15, 2016 at 8:14
  • @GianniDiFalco sorry, im new to Drupal and i've been trying to figure my way around it. And it seems the second argument of the submit hook has the necessary data i need. Thanks a bunch :)
    – Haider Ali
    Commented Jan 15, 2016 at 8:39

1 Answer 1

1

@Haider Ali, Drupal form need to be defined with the $form, $form_state arguments

  function your_form_name($form, &$form_state) { }

For validate and submit function, you need to define them with function name following _validate and _submit respectively. So your validate function should be like

  function your_form_name_validate($form, &$form_state) { }

and submit function:

  function your_form_name_submit($form, &$form_state) { }

If form, and related functions are defined in this way, then only validate and submit function will be called by default. Else they will not be called.

0

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.