First of all, sorry for posting before in a quite old post (https://drupal.org/node/240783). This new post is related to mentioned one.
I have next field in a form
form['aform']['status'] = array('#type' => 'select',
'#title' => t('Status'),
'#options' => array(0 => 'OFF', 1 => 'ON'),
'#value' => $previousIfExisted,
'#required' => TRUE);The question is that this form is same used to add new records in a db table as for editing existing ones. So If editing, the previously selected value for this
filed should be shown. And so $previousIfExisted will be 0 by default and whichever the previuosly selected.
No way men, I cannot get it working.
Some one said (in older versions of drupal in the above mentioned post) that the options array should be of type
array('OFF'=>'OFF', 'ON'=>'ON');Where keys and values are the same. :| Actually thrilling if someone's got keys like 'this is my option one'.
Some one knows what am i doing wrong ?
Comments
You don't have to make the
You don't have to make the keys of the array the same as the values, you just have to know that the key will be the value that is submitted.
Anyways, you didn't show how you set $previousIfExisted, so we can't really say why its not working.
Full-time freelancer, always looking for work.
jaypan.com (my portfolio)
Ok, some more comprehensive
Ok, some more comprehensive info:
function ugmgr_add_cam_form (&$form_state, $cid = NULL) {//krumo($cid);
drupal_set_message('DEBUG: is it cid null? ' . (($!cid) ? 'TRUE' : 'FALSE'));
if(!$cid) {
$addORsave = ADD;
$cid = _ugmgr_get_newcamid();
.....
} else {
$addORsave = UPDATE;
$thecam = _ugmgr_get_cams($cid);
$status = $thecam['status'];
....
}
$form['camopts']['ugmgr_status'] = array('#type' => 'select',
'#title' => t('Status'),
'#options' => array(0 => STOFF, 1 => STON),
'#default_value' => $status,
'#required' => TRUE);
return $form;
}
And the select field aaaaalways show the default value (first value)
Let's close a post which
Let's close a post which never should have existed...
where code above said
$status = $thecam['status'];it should say
$status = $thecam[$cid]['status'];Just a mistake done by bad programmer .... nothing of interest for other drupal developers.
Good night.
What do you get if you add
What do you get if you add this:
<?phpdie('<pre>' . print_r($status, true) . '</pre>');
?>
after
<?php$status = $thecam[$cid]['status'];
?>
Full-time freelancer, always looking for work.
jaypan.com (my portfolio)
The expected value
Never mind man, see my above post, it's solved, I simply forgot I was working with an array of arrays .... that was all ..... my fault
Thanks for help.