Hi,
The only reference I could find to this problem is an 18-month-old post in the forums. I can't get my CCK select list to accept a default value specified by hook_form_alter.
My field type is an integer, select list. I've put a bunch of default values in to mimick the usual Drupal 'weight' fields (from 10 to -10), so I want '0' to be the default. Here's what I've tried, none of which have any effect on the form:
//value as a string
$form['field_faq_weight']['key']['#default_value'] = '0';
//value as an integer, just in case!
$form['field_faq_weight']['key']['#default_value'] = 0;
//value as an array
$form['field_faq_weight']['key']['#default_value'] = array(0 =>0);
I then found this thread: http://drupal.org/node/91759
I tried an integer which would represent a key in the range of my array if it were autonumbered:
//value as a valid auto-numbered key
$form['field_faq_weight']['key']['#default_value'] = 2;
Still nothing! My $form
array looks like this at the moment, which ought to do *something*:
[field_faq_weight] => Array
(
[#tree] => 1
[key] => Array
(
[#type] => select
[#title] => FAQ Weight
[#default_value] => 2
[#multiple] =>
[#options] => Array
(
[10] => 10
[9] => 9
[8] => 8
[7] => 7
[6] => 6
[5] => 5
[4] => 4
[3] => 3
[2] => 2
[1] => 1
[0] => 0
[-1] => -1
[-2] => -2
[-3] => -3
[-4] => -4
[-5] => -5
[-6] => -6
[-7] => -7
[-8] => -8
[-9] => -9
[-10] => -10
)
[#required] => 1
[#description] =>
)
[#weight] => -1
)
My default value is having absolutely no effect on the select list no matter what I do. Why is this?
Thanks!
Comments
Comment #1
James Marks CreditAttribution: James Marks commentedThe short answer is that your array of select options needs to be an associative array consisting of a $key => $value pairs where both the $key and $value contain the option value.
If you hand an indexed array of options to a select list, it will produce select options that use the index as the value. For example, this:
will produce this:
The '#default_value' of 'Red' isn't in the list of values so it won't display 'Red' as the selected choice.
The correct way to do this is to set an associative array of options so that this:
will produce this:
The 'Red' option will be marked as 'selected' because the '#default_value' of 'Red' is in the list of values and can therefore be set as 'selected'.
Hope that helps.
Comment #2
greg.harveyWow, that was a long time ago, but thanks for the detailed reply. It will help someone, for sure. =)
Comment #3
dapperry CreditAttribution: dapperry commentedHow do you do this in code? I have a module where I read the CCK form in, and then (if user is logged in), prefill the fields with the users content profile. This works fine for textfields, but for the life of me I have not figured out how to set default_value for select.
Comment #4
lockev3.0 CreditAttribution: lockev3.0 commentedSorry for being late but ...
That associative array does not seem a good way to refer selectable options.
What if I have something like:
The expected here would be to index colors such as a database model would do :
(1=>'High Red', 'Light grey', ... )
In order to present the text in the select element but choose it thru its index.
And as you say, this ain't valid ? (indeed default_value it's not working for me)
Comment #5
KarenS CreditAttribution: KarenS commentedLet's don't resurrect a two year old issue for another version and try to make any sense out of it. If you have a question about the current code create a new issue with enough detail to tell what you're talking about. No clue what your question is.
Comment #6
nevets CreditAttribution: nevets commentedPlease do not change subject and version (I reset them). You should really post a new issue, not use an old closed one.
Comment #7
dreamsz71 CreditAttribution: dreamsz71 commentedDrupal 7 Form API select element
case: how to set default_value for a select form element (month field) so that Oct is selected as defult ?
step 1: for eg: $month=10;
step 2: $monthOptions=array("1"=>"january",
"2"=>"feb","3"=>"mar","4"=>"apr","5"=>"may",
"6"=>"jun","7"=>"jul","8"=>"aug","9"=>"sep",
"10"=>"oct","11"=>"nov","12"=>"dec");
Step 3:
$form['elementName']['month']=array(
'#type'=> 'select',
'#title' => 'Month',
'#required' => 'true',
'#options' => $monthOptions,
'#default_value' => $month,
);
Comment #8
prsnjtbarman CreditAttribution: prsnjtbarman commented#7 is correct