1

I'm having a little trouble with the terminology here, so I'll explain my problem directly with code samples (trucated for clarity).

myModule.module

Here I create a menu with a link to a form built using a function declared in form_identification.php

[...]
require_once(dirname(__FILE__) . '/form_identification.php');
[...]
function myModule_menu() {
    $weight = 1;
    $items = array(
        'identification' => array(
            'title' => t('Identification'),
            'page callback' => 'identification_callback',
            'access arguments' => array('Acces myModule'),
            'type' => MENU_NORMAL_ITEM,
            'weight' => $weight++
        ),
        [...]
    );
    return $items;
}
[...]
function identification_callback() {
  return drupal_get_form("form_identification");
}
[...]

form_identification.php

This file holds the function which builds the form, called form_identification

function form_identification($form_state) {

    error_log("form_state: " . print_r($form_state, true));
    // always outputs `form_state: Array\n(\n)\n`

    $form = array(
        '#attributes' => array(
            'enctype' => 'multipart/form-data'
        ),
        'sex' => array(
            '#type' => 'textfield',
            '#title' => t('Sex'),
            '#disabled' => true
        ),
        /*[...]*/
        'enregistrer' => array(
            '#type' => 'submit',
            '#value' => t('Enregistrer'),
            '#submit' => array('form_identification_enregistrer')
        )
    );
    return $form;
}

function form_identification_validate($form, &$form_state) {
    /*[...]*/
}

function form_identification_enregistrer($form, &$form_state) {
    /*[...]*/
}

As you can see, I'm printing the contents of $form_state to the log every time form_identification is called, so I can confirm that the function is called on page load and also after validation. The problem is that in both cases, I just get an empty Array (Array\n(\n)\n). I would have expected to get an array full of form info when the function is called after validation. That way, I could have a look at the values entered and decide how to build the form based on them. Is there any way to achieve this? In what situation would $form_state NOT be an empty array?

1 Answer 1

1

You've just got the signature for the form function slightly wrong, it should be:

 function form_identification($form, &$form_state) {

The first arg that's passed to your form function by the form builder is the $form, which for a new form is pretty much always an empty array.

1
  • I would also point out that in the validate and submit callbacks, $form is passed by reference and should be set as an argument like &$form. Commented Nov 14, 2012 at 3:00

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.