17

I have select list with options:

$options = array(0 => '0', 500 => '500', 1000 => '1000'); 

I'm trying to set it's default value to 500:

$form['price']['min']['#options'] = $options; 
$form['price']['min']['#default_value'] = 500;

But this don't work.

3
  • 2
    I ran into the same issue. The problem was that I was not rendering the form using drupal_get_form(), but some other way because of the use case.
    – user1750
    Commented Jul 10, 2012 at 19:48
  • I met the same question!!!!who can help me? Commented Nov 6, 2012 at 4:30
  • 1
    500 should be a '500', string not an integer.
    – saadlulu
    Commented Sep 16, 2013 at 10:13

12 Answers 12

5

I've never seen this problem. That default value should work. I would say try making the key a string and set #default_value as a string. The #options array may be being passed through array_merge somewhere, thus rekeying integer keys. If keys are strings then that shouldn't happen. Currently, your array looks like this:

$options = array(
  0 => '0',
  500 => '500',
  1000 => '1000',
);

But passing that array through array_merge can reorganize those integer keys to be in sequential order:

$options = array(
  0 => '0',
  1 => '500',
  2 => '1000',
);

If that happens then the 500 key simply doesn't exist. This is because array_merge does not like to preserve integer keys, but you can use strings to prevent it. So, I think your array should look more like this:

$options = array(
  '0' => '0',
  '500' => '500',
  '1000' => '1000',
);

Then you can set your #default_value:

$form['price']['min']['#default_value'] = '500';

Otherwise, is there anywhere else where the default_value may be being overridden?

4

Your code should be work, generally use key value as default value, if it not working make the string as keys and use it as default value.

2

Did you try using a string instead of an integer?

$form['price']['min']['#default_value'] = "500"

Maybe also worth a try adding the 'und' key.

1

Did you try using the key out of the array $options to define the default value?
$form['price']['min']['#default_value'] = $options[500];

1
  • If that is done it will technically just pass the value of $options[500], which is (string) '500'.
    – jordojuice
    Commented Sep 2, 2011 at 21:42
1
$options = array(0 => '0', 500 => '500', 1000 => '1000'); 
$form['price'] = array(
          '#type' => 'select',
          '#options' => $options,
          '#default_value' => 500,
        );

In order to set default value in select list we use #default_value parameter. The value of the default_value attribute of the select list would be the key of the select list array.

0

Following code will show the field with its default value as 500.

$options = array(0 => '0', 500 => '500', 1000 => '1000'); 
$form['price'] = array(
          '#type' => 'select',
          '#options' => $options,
          '#default_value' => 500,
          '#title' => 'Price',
        );  
-1

I faced the same problem and I tried giving the index of the array which I want to be the default value and it worked. My answer is similar to Ashok KS but his second line I could not understand.

-1

fixed,we should set default value first of that $option array, if we added last,the array key changed [1]=>science,[2]=>drama,the right should be [192]=>science,[231]=>drama:-)

-1

try:

$form['price']['#default_value'] = 500;

It's work good for me.

-2

The #default_value should have it's arguments in the form as the #value.

$form['price']['min']['#default_value'] = $options['500'];
-4

Just use

$form['price']['min']['#default_value'] = 2 

which means the default value will take the 2eme option

-5

SOLUTION: Figure out that form_select_options doesn't handle #default_value at all. So I used #value property:

$form['price']['min']['#value'] = isset($_REQUEST['price']['min']) ? $_REQUEST['price']['min'] : $my_default_value;
6
  • 2
    Select options do have a default value. Also setting the #value param will make it impossible for the user to change the value.
    – googletorp
    Commented Aug 8, 2011 at 20:57
  • 1
    Look at the code, I set #value to default_value only if there is no any value in #value.
    – breethe
    Commented Aug 10, 2011 at 8:08
  • You always set a value to #value, either $_REQUEST['price']['min'] or $my_default_value. Whatever you put in the #value will be the value of the field regardless of what ever the user inputs.
    – googletorp
    Commented Aug 10, 2011 at 10:33
  • If user inputs some value, this value is send through GET ($_REQUEST['price']['min']). So if user inputs value I set #value to user input, otherwise #value = $my_default_value
    – breethe
    Commented Aug 10, 2011 at 12:01
  • That is the problem. Users can use the select, but it wont matter what they select, the result will be the same.
    – googletorp
    Commented Aug 10, 2011 at 12:25

Not the answer you're looking for? Browse other questions tagged or ask your own question.