Array of fields on an admin settings form
Hi. I have an admin settings form that contains an array of select fields. The problem is that when submitted, the values don't end up in the db and are the select fields go back to having no selected value. The form was working fine as checkboxes, but when the need arose for each label value to have multiple values to choose from (instead of on/off) and I made it an array of fields, as below, it stopped working. $results_tabs = array('' => 'None','a','b','c','d'); $form['myform']['form_text'] = array( '#value' => t('Select a setting for each type'), ); $form['myform']['myform_types'] = array('#tree' => 1); foreach(myform_list_content_types() as $i => $type) { $form['myform']['myform_types'][$type] = array( '#type' => 'select', '#title' => t($type), '#options' => $results_tabs, '#default_value' => $edit['myform']['myform_types'][$i] ); } return system_settings_form($form); Jeff
Ignore the code in the first message. I've boiled down the problem to this: If the array of values I'm populating my select with is $myarray('apple','orange') and a select box should have 'orange' selected when I paint the form, '#default_value' => 'orange' or '#default_value' => 1, neither cause the value showing in the select to be other than the no-selection value. On 4/30/2010 12:57 PM, Jeff Greenberg wrote:
Hi. I have an admin settings form that contains an array of select fields. The problem is that when submitted, the values don't end up in the db and are the select fields go back to having no selected value. The form was working fine as checkboxes, but when the need arose for each label value to have multiple values to choose from (instead of on/off) and I made it an array of fields, as below, it stopped working.
First, use an associative array for your options list, which as you've already discovered is completely wrong in the code you sent out. '#options' => array("orange" => t("orange"),... ) or '#options' => array('or' => t('Orange'))... or '#options' => array('1' => t('Orange'), ...) Second, use only strings for the keys of the associative array (I've seen numeric keys cause some difficulty here in D7 at least) You don't mention what version of Drupal you're working with. -Randy On Fri, Apr 30, 2010 at 12:28 PM, Jeff Greenberg <jeff@ayendesigns.com>wrote:
Ignore the code in the first message. I've boiled down the problem to this: If the array of values I'm populating my select with is $myarray('apple','orange') and a select box should have 'orange' selected when I paint the form, '#default_value' => 'orange' or '#default_value' => 1, neither cause the value showing in the select to be other than the no-selection value.
On 4/30/2010 12:57 PM, Jeff Greenberg wrote:
Hi. I have an admin settings form that contains an array of select fields. The problem is that when submitted, the values don't end up in the db and are the select fields go back to having no selected value. The form was working fine as checkboxes, but when the need arose for each label value to have multiple values to choose from (instead of on/off) and I made it an array of fields, as below, it stopped working.
-- Randy Fay Drupal Module and Site Development randy@randyfay.com +1 970.462.7450
On 4/30/2010 2:58 PM, Randy Fay wrote:
First, use an associative array for your options list, which as you've already discovered is completely wrong in the code you sent out. '#options' => array("orange" => t("orange"),... ) or '#options' => array('or' => t('Orange'))... or '#options' => array('1' => t('Orange'), ...)
Second, use only strings for the keys of the associative array (I've seen numeric keys cause some difficulty here in D7 at least)
You don't mention what version of Drupal you're working with.
Sorry. It's D6. Ok, made sure of all that, so now in the form field part, I have: $form['myform']['mytypes'][$key] = array( '#type' => 'select', '#title' => t($key), '#options' => $results_tabs, '#default_value' => $results_tabs[$val] ); $results_tabs is an associative array, now defined as $results_tabs => array(''=>'none','a'=>'a','b'=>'b'); $key='a' $val='a' When I dpm $forms at the end, it shows, for example, #default_value=>'a' and options contains the key/value pairs from $results_tabs, yet the dropdown when the page paints is 'none'
I find no problems with this code... function form_default_some_form() { $default = ''; $options = array('' => 'none', 'a' => 'a', 'b' => 'b'); $form['myform']['mytypes'][$key] = array( '#type' => 'select', '#title' => 'Example select', '#options' => $options, '#default_value' => $default, ); return $form; } It performs as expected on D6.16. I thought that the use of an empty string for a key was screwy, but it doesn't seem to affect anything. (But remember, that empty($string) will return TRUE where the string has nothing in it, thus affecting lots of code you might not want to affect.) Be careful that you don't have your browser tricking you by autofilling the form. Ctrl-refresh or Shift-refresh or clear the cache. Try another browser. -Randy On Fri, Apr 30, 2010 at 2:41 PM, Jeff Greenberg <jeff@ayendesigns.com>wrote:
On 4/30/2010 2:58 PM, Randy Fay wrote:
First, use an associative array for your options list, which as you've already discovered is completely wrong in the code you sent out. '#options' => array("orange" => t("orange"),... ) or '#options' => array('or' => t('Orange'))... or '#options' => array('1' => t('Orange'), ...)
Second, use only strings for the keys of the associative array (I've seen numeric keys cause some difficulty here in D7 at least)
You don't mention what version of Drupal you're working with.
Sorry. It's D6. Ok, made sure of all that, so now in the form field part, I have:
$form['myform']['mytypes'][$key] = array( '#type' => 'select', '#title' => t($key), '#options' => $results_tabs, '#default_value' => $results_tabs[$val] );
$results_tabs is an associative array, now defined as $results_tabs => array(''=>'none','a'=>'a','b'=>'b'); $key='a' $val='a'
When I dpm $forms at the end, it shows, for example, #default_value=>'a' and options contains the key/value pairs from $results_tabs, yet the dropdown when the page paints is 'none'
-- Randy Fay Drupal Module and Site Development randy@randyfay.com +1 970.462.7450
The example you sent back is correct, though it wouldn't prove itself with $default = '' ... the problem was that the text for default=null was the only value that -did- show. $default would need to be set = to one of the other values to prove out otherwise. That said, it does, and was indeed the browser <large sigh and chuckle>. Thanks, Randy. Jeff On 4/30/2010 5:20 PM, Randy Fay wrote:
I find no problems with this code...
function form_default_some_form() { $default = ''; $options = array('' => 'none', 'a' => 'a', 'b' => 'b'); $form['myform']['mytypes'][$key] = array( '#type' => 'select', '#title' => 'Example select', '#options' => $options, '#default_value' => $default, ); return $form; }
It performs as expected on D6.16. I thought that the use of an empty string for a key was screwy, but it doesn't seem to affect anything. (But remember, that empty($string) will return TRUE where the string has nothing in it, thus affecting lots of code you might not want to affect.)
Be careful that you don't have your browser tricking you by autofilling the form. Ctrl-refresh or Shift-refresh or clear the cache. Try another browser.
The #default_value should be the KEY not the value of that array element. Nancy E. Wichmann, PMP Injustice anywhere is a threat to justice everywhere. -- Dr. Martin L. King, Jr. ________________________________ From: Jeff Greenberg <jeff@ayendesigns.com> To: development@drupal.org Sent: Fri, April 30, 2010 4:41:14 PM Subject: Re: [development] Array of fields on an admin settings form On 4/30/2010 2:58 PM, Randy Fay wrote:
First, use an associative array for your options list, which as you've already discovered is completely wrong in the code you sent out. '#options' => array("orange" => t("orange"),... ) or '#options' => array('or' => t('Orange'))... or '#options' => array('1' => t('Orange'), ...)
Second, use only strings for the keys of the associative array (I've seen numeric keys cause some difficulty here in D7 at least)
You don't mention what version of Drupal you're working with.
Sorry. It's D6. Ok, made sure of all that, so now in the form field part, I have: $form['myform']['mytypes'][$key] = array( '#type' => 'select', '#title' => t($key), '#options' => $results_tabs, '#default_value' => $results_tabs[$val] ); $results_tabs is an associative array, now defined as $results_tabs => array(''=>'none','a'=>'a','b'=>'b'); $key='a' $val='a' When I dpm $forms at the end, it shows, for example, #default_value=>'a' and options contains the key/value pairs from $results_tabs, yet the dropdown when the page paints is 'none'
You're quite correct...bad idea having keys/values be the same in a test...easy for it to go unnoticed! Jeff
participants (3)
-
Jeff Greenberg -
nan wich -
Randy Fay