I am able to get some html form checkbox inputs to print using drupal_render($form['pizza']), but the default values that I want to be checked on page load are ignored. Running $form['pizza'] through form_process_checkboxes() adds '#options' key array elements to the $form['pizza'] array, which is needed to get drupal_render($form['pizza']) to create the html checkbox inputs. If I don't add the '#value' key to the $form['pizza'] array I get the following error:
Notice: Undefined index: #value in form_process_checkboxes() (line 3314 of /var/www/mysite.com/htdocs/includes/form.inc).
The form_process_checkboxes() code uses the '#value' array to add '#default_value' values to the option key array elements that are added to the $form['pizza'] array in the form_process_checkboxes function. Those '#default_value' values do not result in "checked" identifiers being added to the html inputs that are created by drupal_render($form['pizza']). I am not using the standard hook_form functionality because I am creating the checkbox inputs in a views template file so that I can use the inputs along with jquery to control different images that appear on the page.
Is there a way to get the default checkboxes to be checked on page load using drupal_render() without creating the output using drupal_get_form() and the hook_form function? Maybe with multiple '#type' => 'checkbox' $form arrays instead of 1 '#type' => 'checkboxes' $form array? Or by creating the wrapper that is added to the $form['pizza'] array by drupal_get_form() without the hook_form function? I understand that there are other ways to print checked html checkbox inputs in a template file, but at this point I am pretty curious about why the '#default_value' values are ignored by drupal_render after being added in form_process_checkboxes().
$form['pizza'] = array(
'#type' => 'checkboxes',
'#title' => 'Toppings Choices',
'#title_display' => 'before',
'#attributes' => array(
'name' => 'pizza[]',
),
'#value' => array(2 => 'Sausage', 3 => 'Mushrooms'),
'#options' => array(1 => 'Pepperoni', 2 => 'Sausage', 3 => 'Mushrooms'),
'#default_value' => array(2,3),
);
$form['pizza'] = form_process_checkboxes($form['pizza']);
$html = drupal_render($form['pizza']);
print $html;
Comments
#value is not supported for checkboxes
Please check the form api checkboxes.
#value is required by form_process_checkboxes function
If you do not include '#value' in your form array, a notice error is thrown by the form_process_checkboxes() function, and the '#default_value' values of the option key array elements that are added to the $form array by the form_process_checkboxes function() are null. If you don't run the $form array though the form_process_checkboxes() function before drupal_render(), then the form input html elements are not created, so '#value' is in effect required if you wish to render the checkboxes using the form array with drupal_render() and not drupal_get_form().