Hello,
I'm trying to retrieve and repopulate a form so that users could go in and edit a form they previously submitted. The form has many fields, but, as a first step, I tried to set things up so that only the user's saved heading (from a drop-down menu) would show up correctly. I figured if I understood this, I could cobble together the balance of the code. So, I wrote this
function aModule_edit_the_ad_listing($id) {
global $user;
$output = '';
// Retrieve the listing
$listing = db_query("SELECT * FROM {aModule_listing} WHERE `id` = :id", array(':id' => $id))->fetchObject();
if (!$listing) {
drupal_goto('aModule/list_the_ad_categories');
}
// Get the headings and save which one was previously selected
$options_first = aModule_get_headings();
$selected = $listing->heading;
$form['heading'] = array(
'#type' => 'select',
'#title' => 'Heading',
'#options' => $options_first,
// Try to populate the form with the previously selected value - but this doesn't work
// because the FAPI's description for '#default_value' is 'The value of the form element
// that will be displayed or selected initially if the form has not been submitted yet'
// In this case, the form has already been submitted once
'#default_value' => $selected,
'#description' => 'Select a heading for the ad.',
'#required' => TRUE,
'#ajax' => array(
'callback' => 'aModule_categories_callback',
'wrapper' => 'category_second_replace',
),
);
// Debug - the correct heading does show up here
drupal_set_message('Heading should show up here: ' . $listing->heading, 'status', FALSE);
$form['#attributes'] = array(
'enctype' => 'multipart/form-data',
);
$form['submit'] = array(
'#type' => 'submit',
'#value' => t('Submit'),
);
return $form;
}
Unfortunately, the '#default_value' is not being set to '$selected'. The FAPI seems to suggest that this statement is not correct.
So, I wonder what statement is needed in order to set the form back to the value stored in '$selected'. As can be seen, the value of
$listing->heading
is indeed being output correctly by the following debug statement
drupal_set_message('Heading should show up here: ' . $listing->heading, 'status', FALSE);
Any nudge in the right direction would be greatly appreciated.
Thank you,
Comments
change : <?phpfunction
change :
to :
and add a logic to the #default_value :
then add logic to form_submit function to store the $form_state['storaged_heading'] from $form_state['values']
--------------------------------------------------------------------------------------------------------
if you can use drupal why use others?
VicTheme.com
Thank You
Hi,
Thanks so much for your useful reply. Unfortunately, I haven't yet had the time to check out what you've suggested yet. I'll be sure to report back and keep you posted once I do.
Thanks again for all your help. I really appreciate it.
Still Not Working
Hi,
I made all the changes you suggested very carefully.
The following line doesn't seem to work
No matter what I set '#default_value', it always reverts to some other, default value - not the value we're explicitly setting ($form_state['storaged_heading']). As I mentioned in my first post, the FAPI states that #default_value is:
"The value of the form element that will be displayed or selected initially if the form has not been submitted yet. Should NOT be confused with #value, which is a hard-coded value the user cannot change!"
In this case, the form has already been submitted once - and we're trying trying to retrieve the set values; so I wonder if that is why that line is not working.
Any further help and guidance is appreciated.
P.S: Also, if it helps at all, this is a drop-down menu: the list of headings are first populated and then the previously selected one is what we'd like to display. This is not a 'text field' that we're trying to populate.