欢迎各位兄弟 发布技术文章

这里的技术是共享的

You are here

drupal checkboxes

shiping1 的头像
下面是自己亲自做的关于 修改checkboxes的有效的方法 有大用
因为checkbox 在不选中的情况下,$form_state也有值
不过不选中时,值为0,不知道什么原因 但是现在我自己通过这种方法 可以实现
剔除掉值为0的选项.
function shipingzhongcustomtwo_form_alter(&$form, &$form_state, $form_id){
      if($form_id == 'product_kit_node_form'){
                  //var_dump($form["base"]["products"]);exit;
          $form["base"]["products"]['#type']='checkboxes';
          //$form["base"]["products"]["#options"]=array(1=>'aaa',2=>'bbb',3=>'ccc',4=>'ddd');
          $form['#after_build'][] = 'shipingzhongcustomtwo_product_kit_after_build';
          //$form["base"]["products"]['#attributes'] = array('name'=>'productsssss');
      }
}
//这个目的是去除掉未选中的值(未选中的话 值就为0)
function shipingzhongcustomtwo_product_kit_after_build($form, &$form_state){
      $products = $form_state['values']['products'];
      foreach($products as $key=>$value){
           if(empty($value)){
               unset($products[$key]);
           }
      }
      $form_state['values']['products'] = $products;
      return $form;
}






$options = array();
$options["1"] = "One";
$options["2"] = "Two";
$options["3"] = "Three";
$options["4"] = "Four";
$form['ios'] = array(
  '#title' => t(''),
  '#type' => 'checkboxes',
  '#description' => t(''),
  '#options' => $options,
  '#default_value' => array("1", "2", "3")
); 
来自 http://drupal.stackexchange.com/questions/42571/how-to-check-a-checkbox-by-default-in-froms-api



Try changing your code to the following, no need for the call to form_process_checkboxes.

function my_module_block_view($delta='') {
  $block = array();
  switch ($delta) {
    case 'myform':
    $block['content'] = drupal_get_form('mymodule_form');
    break;
  }
  return $block;
}
//下面这个未发现起作用
function mymodule_form() {

  $form = array();
  $form['tests_taken'] = array(
    '#type' => 'checkboxes',
    '#options' => drupal_map_assoc(array(t('SAT'), t('ACT'))),
    '#title' => t('What standardized tests did you take?'),
    '#attributes' => array(
      'name' => 'example-checkboxes[]'
    )
  );
  dsm($form);
  return $form;
}

来自 http://drupal.stackexchange.com/questions/65011/accessible-checkboxes-using-drupals-form-api


 

You can use the new Form API feature, #states to achieve this.

Note that, for ease of use, I'm wrapping the conditional checkboxes with a fieldset.

<?php
  $form['categories']['templates']['parent1'] =   array(
    '#type'         =>  'checkboxes',
    '#title'        =>  t('Select the appropriate actions for the form based on the indicated template'),
    '#options'      =>  array(1 => 'Parent#1'), // Note this! You can't use 0 as a key in #options.
    '#multiple'     =>  TRUE,
  );
  $form['categories']['templates']['parent1']['wrapper'] = array(
    '#type' => 'fieldset',
    '#title' => t('Options for @option', array('@option' => 'Parent#1')),
    '#states' => array(
      'visible' => array(
        ':input[name="parent1[1]"]' => array('checked' => TRUE), // We use "parent1" and its Nth option. in above field, See #options in above checkboxes field.
      ),
    ),
  );
  $form['categories']['templates']['parent1']['wrapper']['actions']  =   array(
    '#type'         =>  'checkboxes',
    '#options'      =>  array("P1 - option#1", "P1 - option#2"), // This is also wrong. You must define a key => value pair for #options or somehow avoid using 0 as the key.
    '#multiple'     =>  TRUE,
  ); 
?>

You might want to use #tree => TRUE in the fieldsets to avoid Drupal from merging same keys' values together.

Also, you won't need #multiple in checkboxes field type.

Update

Example with node types:

<?php
$node_types = node_type_get_names();
$form['categories']['templates']['parent1'] =   array(
  '#type'         =>  'checkboxes',
  '#title'        =>  t('Select the appropriate actions for the form based on the indicated template'),
  '#options'      =>  $node_types,
);
foreach ($node_types as $type => $label) {
  $form['categories']['templates']['node-options'.$type] =   array(
  '#type'         =>  'checkboxes',
  '#title'        =>  t('Options for @type', array('@type' => $label)),
  '#options'      =>  array("Parent#1"),
  '#multiple'     =>  TRUE,
  '#states' => array(
    'visible' => array(
      ':input[name="parent1['.$type.']"]' => array('checked' => TRUE), // We use "parent1" and its Nth option. in above field, See #options in above checkboxes field.
    ),
  ),
);
}

来自 http://stackoverflow.com/questions/14878783/drupal-form-api-checkboxes-hierarchy



When you use #options on a FAPI element the value passed to the $form_state is the array key, so you don't need to use array_keys().

I'm not sure why you're using checkboxes for a yes/no, usually one would use a simple checkbox element. However if that's really what you want to do:

  1. Your #options can't contain on option with 0 as the array key, it will be automatically filtered out and you'll never know if that option has been checked.

  2. You should use $heating_options_chosen = array_filter($form_state['values']['heating'] to get the selected checkbox options.

I honestly think your code should look like this though:

$form['checklist_fieldset']['heating'] = array(
 '#type' => 'checkbox',
 '#title' => t('Heating options'),
 '#options' => array(
   '1' => t('Yes'),
   '0' => t('No')
  ),
  '#description' => t('Heating details.')
); 
$heating_checked = $form_state['values']['heating'] == 1;

来自 http://stackoverflow.com/questions/9955127/cannot-get-checkboxes-value-using-drupal-form-api

 

普通分类: