drupal #default_value not applied in select option in drupal_render
Asked Answered
T

4

5

I try to print a drupal 'select option' element in a form .I think drupal_render not applying #default_value.every thing is ok except #default_value not applied.
where is the problem?anybody know how i can do this? do #default_value accept string value?

this is pseudo of my codes:

function test_menu(){
$items=array();

    $items['admin/config/regional/test']=array(
    'title' => 'test',
    'description' => t('test'),
    'page callback' =>'drupal_get_form',
    'page arguments' => array('test_function'),

);
$items[]=array();
return $items;
}


function test_function(){
 $header = array
  (
  'test1' => t('test1'),
  'test2'=> t('test2'),
  );
 $a=(1,2,3);
 $$options=array();
 foreach($a as $i=>$v)
  {
    $f['type'] = array(
   '#type' => 'select',
   '#options' => array(1,2,3,4),
   '#default_value'=>1,
    );
$options += array($name=>array( 'test1' => $v,
   'test2'=> drupal_render($f['type']) ,
  }
   $form['table'] = array
   (
   '#type' => 'tableselect',
   '#header' => $header,
   '#options' => $options,
   '#multiple' => FALSE
   //'#empty' => t('No users found'),
   );
   $form['submit'] = array
       (
   '#type' => 'submit',
   '#value' => t('Submit'),
    );
   return $form;
 }    

I test textfield but its also not work and not accept #default_value in drupal_render

    $f['test3']=array(
    '#type'=>'textfield',
    '#title'=>'test3',
    '#default_value' =>'aaa',
);

I suppose this is beacuse using drupal_render .anybody have a solution?

Team answered 19/7, 2012 at 5:22 Comment(1)
One thing people should be aware of when working with this is that the autocomplete attribute should be set to 'off' or Firefox wont show the selected option as selected.Benefice
T
11

In Drupal_render 's used in drupal_get_form , #default_value not set use must use #value instaed of it.

$f['type'] = array(
'#type' => 'select',
'#options' => drupal_map_assoc(array('1','2','3','4')),
'#value'=> '1',
);
Team answered 20/7, 2012 at 14:25 Comment(3)
This is dangerous as user will not be able to override the submitted value (Description: Used to set values that cannot be edited by the user. Should NOT be confused with #default_value, which is for form inputs where users can override the default value.) api.drupal.org/api/drupal/…Bedrail
Thanks so much, this answer is correct and solved my stuckBile
@TrungLeNguyenNhat you're welcome, I happy to seen my answer could help another guys after some years :)Team
M
5

The following code doesn`t work:

$form['title'] = array(
'#type' => 'select',
'#options' => drupal_map_assoc(array(t('opt1'), t('opt3'), t('opt4'), t('opt5'), t('opt6'))),
'#default_value' => array($default_value_key));


$form['title1'] = array(
'#type' => 'select',
'#options' => drupal_map_assoc(array(t('opt1'), t('opt3'), t('opt4'), t('opt5'), t('opt6'))),
'#default_value' => array($default_value_key));

return $form;

But then i did the following:

$form['group'] = array('#tree' => TRUE);

$form['group']['title'] = array(
'#type' => 'select',
'#options' => drupal_map_assoc(array(t('opt1'), t('opt3'), t('opt4'), t('opt5'), t('opt6'))),
'#default_value' => array($default_value_key));


$form['group']['title1'] = array(
'#type' => 'select',
'#options' => drupal_map_assoc(array(t('opt1'), t('opt3'), t('opt4'), t('opt5'), t('opt6'))),
'#default_value' => array($default_value_key));

return $form;

And default values now works.

Middling answered 12/10, 2012 at 9:7 Comment(1)
Adding the $form['group'] = array('#tree' => TRUE); worked for me tooDeneb
I
2

I have the same problem. finally, i found one method. As you said, the default_value does not work. So make the default_value fixed as 0. and change the options array, put the default value on the top.

Indiscriminate answered 22/1, 2014 at 21:37 Comment(0)
M
0

If you look at the example from Drupal's Form API, you'll see that the #options setting takes a key-value pair array and in the #default_value, you should specify the key of the default value, not the string value.

Also, according to documentation for the #options setting, #options is expecting String values. So your select should be more like:

$f['type'] = array(
   '#type' => 'select',
   '#options' => drupal_map_assoc(array('1','2','3','4')),
   '#default_value'=> '1',
);
Mullion answered 19/7, 2012 at 12:10 Comment(1)
.It's not working with drupal_render. In my orgin code i set array keys . the problem is 'drupal_render' that use in drupal_get_form not set #default_value.Team

© 2022 - 2024 — McMap. All rights reserved.