[development] Forms API -- default values without anything in database

Syscrusher syscrusher at 4th.com
Sat Nov 24 22:21:30 UTC 2007


I've been pounding my head against this one all afternoon, and have been all
through the documentation, and am utterly stymied. I can't even find an extant
module that does this, but I'm certain _someone_ has already invented this
wheel.

I'm working with a fresh CVS checkout of Drupal 5.x.

I have a module that creates and maintains a database table that is NOT related
in any way to nodes. I am implementing a search function that presents a form
allowing users to enter the desired match values, then presents a table with
a list of matching records. No problem on any of this.

The problem is how do I get the form to REDISPLAY the search values when it has
been submitted, so that they become the default values for the search fields
next time?

My form creation code looks similar to this:

function mymodule_form_query($form_values = null) {
        $form = array(
                'basic' => array(
                        '#type' => 'fieldset',
                        '#tree' => false,
                        '#title' => t('Basic search'),
                        'last_name' => array(
                                '#type' => 'textfield',
                                '#title' => t('Last name'),
                                '#size' => 35,
                                '#maxlength' => 35,
                                '#weight' => -10,
                                '#value' => $form_values['last_name'],
			),
	[...snip...]
	return $form;
}

and the page that processes the query has a menu callback akin to this:

function mymodule_query_page() {
        $form_values = $_POST;
        $html = drupal_get_form('mymodule_form_query', $form_values);
        return $html;
}

Now, if I do a var_dump() in the page function, it shows that the last_name field
is set in the $_POST array when the form is submitted. But it seems to have no
effect in the mymodule_form_query function. That function is called TWICE, once with
values in $_POST and again with nothing. It seems that the only time it pays
attention to its #value (or #default_value -- I've tried both) options is the
second time it's called, that is, when $form_values is empty. Ergo, no values
for the fields.

I've tried this as well, with no effect:

function mymodule_form_query_submit($form_id, $form_values) {
	$GLOBALS['mymodule_query'] = $form_values;
}

and then querying $GLOBALS['mymodule_query']['last_name'] in the mymodule_form_query()
function to obtain the value. Although trace code within the submit shows that
the value is in fact present there, somehow my global variable is erased or
ignored by the time it gets to the form building process.

Oddly, if I change the form to GET rather than POST method, the $_GET variable
behaves the way I would have expected $_POST to work, and the fields are filled
in. But it's really ugly to do it that way, because among other things the URL
then contains the form_token and may be bookmarked by users as such -- Not Good.

What am I doing wrong here? Someone has to have done this before, so I'd
greatly appreciate any assistance. What's the RIGHT way to do this? Is there
a place where Drupal actually nukes all global variables during initialization,
and where it erases $_POST?

Kind regards,

Scott

-- 
-------------------------------------------------------------------------------
Syscrusher (Scott Courtney)          Drupal page:   http://drupal.org/user/9184
syscrusher at 4th dot com            Home page:     http://4th.com/   


More information about the development mailing list