drupal #default_value not applied in select option in drupal_render

Yuseferi picture Yuseferi · Jul 19, 2012 · Viewed 8.3k times · Source

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?

Answer

Yuseferi picture Yuseferi · Jul 20, 2012

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',
);