0

I'm creating a form using Drupal's form API. This form includes about 50 or so checkboxes that I'm creating using the type 'checkbox'. The checkboxes are grouped using several fieldset elements. The checkbox is created from a look using an XML file that decides what checkboxes will appear.

$ageKey = '08';
$course = 'y';

$form['events'][$ageKey][$course] = array(
  '#type' => 'fieldset',
  '#title' => 'Set of checkboxes'
}

foreach($xCourse->event as $event) {
  $eventKey = (int)$event['distance'] .'-' . (int)$event['event'];
  $form['events'][$ageKey][$course][$ageKey . ' ' . $course . ' ' . $eventKey] = array(
    '#type' => 'checkbox',
    '#title' => (int)$event['distance'] . ' ' . $top5->dispStroke((int)$event['stroke']),
    '#default_value' => 1,
  );
}

I'm using hook_submit to process the form

function import_form_submit($form, &$form_state) {
  $checkboxes = $form_state['values'];

  //do stuff here
}

However, I find that the value for all my checkboxes is 0, regardless if the checkbox was unchecked by me or not. If I remove '#default_value' => 1 from the checkbox definition, none of the checkboxes are checked by default. The values array also appears to ignore the hierarchy I set up.

How do I get the checked state of the checkbox in my submission code? What am I missing here?

3 Answers 3

1

I found the answer. I added '#tree' => TRUE, to the fieldset and suddenly things are working correctly. I think because of the eventKeys were repeated so overwriting others. By adding the tree attribute, all the checkboxes were seperated into their respective fieldsets in the $form_state['values'] array.

1

It would be better if you use the variable_get($ageKey . ' ' . $course . ' ' . $eventKey) for setting default values.

For more you can see this

0

I searched for a long time to find a comprehensive answer for this and arrived at this solution:

First declare your checkboxes associative array. This particular array has a fieldset component which uses the Drupal 7 #states property. My project is a module for D7 which uses FAPI to create a node and save it to the database unpublished.

    $form['major']['field_intern_agency_type'] = array(
    '#type' => 'checkboxes',
    '#title' => t('Agency Type'),
    '#options' =>  drupal_map_assoc(array(
      t('Arenas, Stadiums, Venues, and Convention Centers'),
      t('Corporate Enterprises, Communications, Event Planning, and Travel'),
      t('Municipalities, Visitors’ Bureaus, and Departments of Parks & Rec'),
      t('Museums, Aquariums, Zoos, and Historical and Cultural Sites'),
      t('National and State Parks, DCNR, and Fish and Boat Commissions'),
      t('Nature Centers, Wildlife Reserves, and Conservancy'),
      t('Nonprofit Organizations and Festivals'),
      t('Outdoor Adventure and Experiential Education'),
      t('Resorts, Hotels and Hospitality, Amusement Parks, and Country Clubs'),
      t('Sports Management, Teams, Sport Associations, Merchandising, and Fitness'),
      t('Summer Camps, YMCAs, Family/Youth Recreation, Campgrounds, and Military'),
      t('Therapeutic Recreation'),
      t('Universities and Colleges including Athletics'),
      )
    ),
    '#default_value' => array(),
    '#states' => array(
     'visible' => array(   // action to take.
       ':input[name="field_majors"]' => array('value' => 'Recreation Park and Tourism Management'),
         ),
       ),
  );

I use the array_values() function to turn the form array (specifically the checked checkboxes) into a simple numerically indexed array, then use foreach() to iterate over the array and copy each value to the new checkboxes array which is then saved when the form is submitted with whatever boxes are checked being saved also. Hope this helps someone!

$agencytypevalues = array_values($form['major']['field_intern_agency_type']['#value']);
$i = 0;
foreach ($agencytypevalues as &$value) {
  $node->field_intern_agency_type[$node->language][$i]['value'] = $value;
  $i++;
};
2
  • Can you link the source from where you have this solution?
    – kenorb
    Commented Jan 6, 2016 at 16:25
  • The documentation for D7 FAPI is here: api.drupal.org/api/drupal/… -- however this solution has not yet been published on Drupal.org. Commented Jan 6, 2016 at 16:36

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.