1

I am working with a form which contains list of checkboxes based on country. checkboxes are rendered correctly. But on submit, I don't get checkboxes' correct values. It always gets submitted as NOT CHECKED. My code is as below:

    foreach($fields as $id=>$title) {
          $form['test'.$code]['fields-'.$code] = array(
              '#type' => 'checkboxes',
              '#options' => drupal_map_assoc(array_keys($fields)),
              '#title' => t('Title'),
              '#attributes' => array('name' => 'field-'.$code.'-'.$id),
          );
        }

    function config_submit($form, &$form_state) {
      var_dump($form_state['values']);
      exit;
    }

outputs :

array (size=13)
  'fields-dk' => 
    array (size=7)
      'title' => int 0
      'reason' => int 0
      'doc-reference' => int 0
      'description' => int 0
      'upload-claim-file' => int 0
      'product-detail' => int 0
      'barcode-num' => int 0

Why is it returning always as 0? What am I missing?

4
  • I'm not sure why you're using "checkboxes" for a yes/no, usually one would use a simple checkbox element.
    – user52318
    Commented Jan 11, 2016 at 7:31
  • @Vicky8888, its not Yes/No.. Its a list of fields. User can select which fields he wants to display.
    – meen
    Commented Jan 11, 2016 at 7:56
  • 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().
    – user52318
    Commented Jan 11, 2016 at 7:56
  • @Vicky8888, I have just manipulated the array there to get expected titles for checkboxes
    – meen
    Commented Jan 11, 2016 at 8:53

2 Answers 2

2

While we can't see how your form is built completely, it might be that your custom attributes are affecting the element "name" in the form state.

i.e change

'#attributes' => array('name' => 'field-'.$code.'-'.$id),

to

'#attributes' => array('data-name' => 'field-'.$code.'-'.$id),
4
  • Hey, this does not give me 1 if checked but returns data-name if checkbox is selected... So now I can differentiate checked and non-checked checkboxes in Backend. Thanks.
    – meen
    Commented Jan 11, 2016 at 9:04
  • Sounds like you're using JS or Ajax somewhere on the form? In any case, glad that helped. Commented Jan 11, 2016 at 9:15
  • No JS or Ajax is used. Its simple configuration page.
    – meen
    Commented Jan 11, 2016 at 9:18
  • Ok, keep in mind checkboxes doesn't always return 1 for the value, it will return the key as the value instead of 0 Commented Jan 11, 2016 at 9:20
0

Please verify the submission data with below:

$form_state['values']['checkboxes']; 

or

foreach($form_state['values']['checkboxes'] as $key=>$value){
  // your logic
}

I have used the below form field for checkbox

$form['checkboxes'] = array(
      '#type' => 'tableselect',
      '#header' => $build_table['#header'],
      '#options' => $build_table['#rows'],
      '#empty' => t('No records found.')
    );
1
  • it does not work :(
    – meen
    Commented Jan 11, 2016 at 8:54

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.