Defining #default_value for a select form item
zetxek - November 19, 2007 - 10:10
I have an array where there are some options already loaded (it's a nodereference field that allows referencing only the nodes that are from one content type).
If I
<?php
print_r($form['field_recurso']);
?><?php
Array ( [#tree] => 1 [nids] => Array ( [#type] => select [#title] => recurso [#default_value] => Array ( [0] => 0 ) [#multiple] => 0 [#size] => 0 [#options] => Array ( [417] => Canón de vídeo NEC [446] => Canón de vídeo PLUS [447] => Portátil PC [418] => Sala de Xuntas ) [#required] => 1 [#description] => [#post] => Array ( ) [#programmed] => [#tree] => 1 [#parents] => Array ( [0] => field_resource [1] => nids ) [#weight] => 0 [#processed] => [#attributes] => Array ( ) [#input] => 1 [#name] => field_resource[nids] [#id] => edit-field-resource-nids [#value] => Array ( [0] => 0 ) [#sorted] => 1 ) [#weight] => -10 [#post] => Array ( ) [#programmed] => [#parents] => Array ( [0] => field_resource ) [#processed] => [#sorted] => 1 )
?>I tried several options to change default value, and the value of the array item changes, but it makes no effect.
I tried setting #default_value to a integer (418, 447, the nids of the referenced nodes), to an array (containing the nid and the node title: arrray(418=>Sala de Xuntas), to the node title (Sala de Xuntas)... but I had no luck.
Any clue?
For reference, my whole form looks like this:
<div id="resource"><?php print drupal_render($form['field_resource']); ?></div>
<div id="start"><?php print drupal_render($form['start_date']); ?></div>
<div id="end"><?php print drupal_render($form['end_date']); ?></div>
<div id="description"><?php print drupal_render($form['field_description_0']); ?></div>
<?php
print drupal_render($form['form_token']);
print drupal_render($form['form_id']);
// print drupal_render($form['preview']);
print drupal_render($form['submit']);
print drupal_render($form['delete']);
?>
Nearly there
The default_value should be an array of the option's keys - so in your above example it should be
array(418,447);or for a single value
array(418);Cheers,
Mike
Like books? Check out booktribes the new (Drupal based) community for book lovers
from Computerminds
Thanks for the reply. I
Thanks for the reply.
I tried making
<?php$form['field_resource']['#default_value']=array(418);
?>
<?php[#default_value] => Array ( [0] => 0 )
?>
I tried too making
<?php$defvalue = array(); $defvalue[] = 418; $form['field_resource']['#default_value']=$defvalue;
?>
I guess I'm having another problem, now setting the value to the form...
Did you sort this one out?
Did you sort this one out? I'm having the same problem except that I am able to set #default_value in the form. The problem is that whatever I set this to has no effect on the dropdown when it is rendered.
Like you I have a select box with the options populated by taxonomy terms, with tids as values and names displayed. I have several of these select boxes in my form and I need to set the default value of each to match the current values in the database.
I have tried:
'#default_value' => $tid
'#default_value' => $name
'#default_value' => array( $tid )
'#default_value' => array( $name )
'#default_value' => array( $tid => $name )
I have also tried setting the value to a literal rather than a variable - nothing works. I have also tried to bypass setting #default_value and attaching the 'selected' attribute directly to the option, but setting attributes on options does not seem to be possible either. Short of re-writing the entire theme engine for select boxes I don't know what I can do.
This is a simple single page form, not a multi-part one. I am using Drupal 5.
Thanks,
Scott
Having the same problems here
Scott - I don't know why, but I can't get this to work either. Your solution would be nice.
I have some VERY similar code on another module which works fine. This one doesn't. I'm in a multi-step form for this one though.
If i turn #multiple to FALSE and then just give one item, it works fine. There seems to be an intermittent bug with this multiple default_value issue.