drupal form api checkboxes
Asked Answered
B

1

7

I am using drupal form api and using checkboxes. I am getting problem in default checked values with it. following is the code snippet...

$result = db_query("SELECT nid, filepath FROM {content_type_brand}, {files} WHERE content_type_brand.field_brand_image_fid
= files.fid");
        $items = array();
        while ($r = db_fetch_array($result)) {
                array_push($items, $r);
        }
        $options = array();
        foreach( $items as $i ) {
            $imagePath = base_path().$i['filepath'];
            $options[$i['nid']] = '<img src="'.$imagePath.'"></img>';
        }
        $form['favorite_brands'] = array (
            '#type' => 'fieldset',
            '#title' => t('Favorite Brands'),
            //'#weight' => 5,
            '#collapsible' => TRUE,
            '#collapsed' => FALSE,
        );
         $form['favorite_brands']['brands_options']
= array(
            '#type' => 'checkboxes',
            '#options' => $options,
            '#default_value' => $options_checked,// $options_checked is an array similar to $options but having elements which need to be checked by default...
            '#multicolumn' => array('width' => 3)
        );

but values are not checked by default... can anyone help what I am missing??

Thanks

Bionomics answered 10/2, 2011 at 8:3 Comment(1)
where is $options_checked defined? The problem is that the provided snippet does not define $options_checked in any way, therefore no checkboxes get checked by defaultDogooder
R
7

Your $options_checked array should not be in the same format as your $options array. Your $options array contains nid => img tag pairs. Your $options_checked array should simply contain the nid values of the options that should be checked by default:

$options_checked = array(8,17);
Rebroadcast answered 14/4, 2011 at 2:0 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.