Forms API -- default values without anything in database
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/
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.
I would recommed using form['#redirect'] = FALSE and an #after_build function akin to how node previews are done.
On Saturday 24 November 2007 18:25, Karoly Negyesi wrote:
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.
I would recommed using form['#redirect'] = FALSE and an #after_build function akin to how node previews are done.
Exactly what I needed! I'd actually already stumbled on the #after_build as I was looking at node previews for an example. The part I was missing was the setting of #redirect to FALSE. Thaks very much! Also thanks to Marco Carbone and David Sterratt (egfrith) for their replies (the latter off-list), which were also interesting and useful. The piece that eluded me was that I didn't realize a redirect was being done even if I didn't return a URL from foo_submit(). It never occurred to me that it would redirect to itself, thereby losing _POST and all the GLOBALS[...] but not _GET, because that's of course part of the redirect URL. It all makes sense now. Thanks again, folks! I will file this thread in my Drupal reference library. Kind regards, Scott -- ------------------------------------------------------------------------------- Syscrusher (Scott Courtney) Drupal page: http://drupal.org/user/9184 syscrusher at 4th dot com Home page: http://4th.com/
The piece that eluded me was that I didn't realize a redirect was being done even if I didn't return a URL from foo_submit(). It never occurred to me that it would redirect to itself
You can also return FALSE to avoid this. The common case is that you want a redirect so that's why it does so.
See, e.g., http://drupal.org/node/56129. -marco On Nov 24, 2007 2:21 PM, Syscrusher <syscrusher@4th.com> wrote:
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/
Sorry for replying so late, but I was just catching up on some old mail and noticed this. I wanted to point out another gotcha: On Nov 24, 2007, at 2:21 PM, Syscrusher wrote:
'last_name' => array( '#type' => 'textfield', '#title' => t('Last name'), '#size' => 35, '#maxlength' => 35, '#weight' => -10, '#value' => $form_values['last_name'], ^^^^^^^^
evil. you mean '#default_value' here. ;) that said, sure, #after_build is nice and all. enjoy, -derek (dww)
participants (4)
-
Derek Wright -
Karoly Negyesi -
Marco Carbone -
Syscrusher