2

画像をアップロードするdrupalのフォームがあり、チェックボックスがいくつかあります。フォームは次のとおりです。

$form['checklist_fieldset'] = array(
    '#type' => 'fieldset',
    '#title' => t('Check List'),
    '#collapsible' => FALSE,
    '#collapsed' => FALSE,  
  );
$form['checklist_fieldset']['heating'] = array(
   '#type' => 'checkboxes',
   '#title' => t('Heating options'),

   '#options' => array(
  '0' => t('Yes'),
  '1' => t('No')
  ),
   '#description' => t('Heating details.')
  );

これが私の送信機能で、画像のアップロードを処理し、チェックボックスの値も取得しています。成功メッセージが表示され、画像がアップロードされますが、チェックボックスの値が取得されません。

function property_add_view_submit($form,&$form_state){
$validators = array();



if($file = file_save_upload('p_file1',$validators,file_direcotry_path)){
$heating = array_keys($form_state['values']['heating']);
drupal_set_message(t('Property Saved! '.$heating));
dpm( $form_state['values']['heating']);
}
4

2 に答える 2

5

FAPI要素で使用する場合#options、に渡される値$form_state 配列キーであるため、を使用する必要はありませんarray_keys()

checkboxesはい/いいえに使用している理由がわかりません。通常は単純なcheckbox要素を使用します。しかし、それが本当にあなたがやりたいことであるなら:

  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;
于 2012-03-31T11:44:03.510 に答える
0

If I have checkbox Friends and options are like

[ ] abc  
[ ] def  
[ ] ghi  
[ ] jkl 

And I want to know which options user have marked, then use below function.

if ($form_state->getValue('friends') != NULL) {

  foreach ($form_state->getValue('friends') as $key => $value) {

    if ($value != 0) {
      $friends = $friends . ", " . $key;
      $friends = substr_replace($friends, "", 0, 1);
    }
  }
}

If user has chosen abc and ghi then you will get 1,3 as result in $friends

If you wanted to know the value then use $friends = $friends.", ".$value;

it worked for me..hope it will help you as well :)

于 2018-10-01T08:34:51.923 に答える