How do you prepopulate a checkbox (make it checked)? I tried edit[field_name]=checked with no luck.

CommentFileSizeAuthor
#12 prepopulate_usage.patch659 bytesmalcomio

Comments

add1sun’s picture

pretty sure you need to use edit[field_name]=1

add1sun’s picture

Status: Active » Fixed

no response. closing

mikeytown2’s picture

Version: 5.x-1.3 »

in 6.x-2.x-dev
with content that looks like this

mine:
1
2
3
4
5
<div class="form-item">
 <label>mine: </label>
 <div class="form-checkboxes"><div class="form-item" id="edit-field-mine-taxonomy-value-43-wrapper">
 <label class="option"><input name="field_mine_taxonomy[value][43]" id="edit-field-mine-taxonomy-value-43" value="43" class="form-checkbox" type="checkbox"> 1</label>
</div>
<div class="form-item" id="edit-field-mine-taxonomy-value-44-wrapper">
 <label class="option"><input name="field_mine_taxonomy[value][44]" id="edit-field-mine-taxonomy-value-44" value="44" class="form-checkbox" type="checkbox"> 2</label>
</div>

<div class="form-item" id="edit-field-mine-taxonomy-value-45-wrapper">
 <label class="option"><input name="field_mine_taxonomy[value][45]" id="edit-field-mine-taxonomy-value-45" value="45" class="form-checkbox" type="checkbox"> 3</label>
</div>
<div class="form-item" id="edit-field-mine-taxonomy-value-46-wrapper">
 <label class="option"><input name="field_mine_taxonomy[value][46]" id="edit-field-mine-taxonomy-value-46" value="46" class="form-checkbox" type="checkbox"> 4</label>
</div>
<div class="form-item" id="edit-field-mine-taxonomy-value-47-wrapper">
 <label class="option"><input name="field_mine_taxonomy[value][47]" id="edit-field-mine-taxonomy-value-47" value="47" class="form-checkbox" type="checkbox"> 5</label>

</div>
</div>
</div>

set the equal to the number after the value bracket. example
...?edit[field_mine_taxonomy][value][47]=47

Status: Fixed » Closed (fixed)

Automatically closed -- issue fixed for two weeks with no activity.

OptimusPrime23’s picture

$form['item'][item2]['check'] = array(
'#type' => 'checkbox',
'#default_value' => 1,
'#prefix' => "

'#suffix' => "

",
);

the default_value makes in checked hope this resolves ur issue

deviantintegral’s picture

If you want to make it work for checkboxes, and not just a single checkbox, you set the value to be the value of the checkbox. Example:

user/register?edit[field_customer_segment][value][Home%20Owner]=Home%20Owner

giorgio79’s picture

Status: Closed (fixed) » Active

Not sure this does not seem to be working for me

wgrunberg’s picture

I am also having difficulties checking a CCK "Single on/off checkbox" field (double post from #799348: Prepolutaing CCK "Single on/off checkbox" field?).

Given the following form element and some Devel exploration:
<input type="checkbox" class="form-checkbox" value="on" id="edit-field-core-template-value" name="field_core_template[value]">

I have unsuccessfully tried:
edit[field_core_template]=on
edit[field_core_template]=1
edit[field_core_template][value]=on
edit[field_core_template][value]=1
edit[field_core_template][0][value]=on
edit[field_core_template][0][value]=1
etc.

I ended up converting the check-box into a select list. No problems there. This is a great module.

electblake’s picture

Status: Active » Needs work

I found a solution!

So, I was attempting to select a checkbox.

usually I would follow the name of the input to populate the field. (that is the html parameter name="")

But in my case (of organic groups) it was more complicated.

  1. I opened prepopulate.module and dpm($form) (its part of the devel module, you could simply print_r) in the hook_form_alter() implementation to see what the module sees and find the poper values to change.
  2. on line 63 I added.
    if ($form['#type'] == 'checkboxes') {
       $getslice = array($getslice);
    }
    
  3. my query string (again for organic groups) became: ?edit[og_nodeapi][visible][og_groups]=1

    note, this is ?edit[field_name]=value of checked box.

The prepopulate module does not check if the $form['#default_value'] accepts an array or a plain string, so as checkboxes field types are looking for an array, our values would be ignored.

I'm sure there is a more elegant solution, and this obviously only solves the problem for checkboxes not any other array casted default_value field types.

But its something that we can finally build on :)

dawehner’s picture

It's already possible when you use such a syntax

[og_nodeapi][visible][og_groups][$nodeid]=1
TimelessDomain’s picture

Version: » 6.x-2.x-dev

How can this be done in Drupal 7? For ex. a taxonomy reference field

does this need to be added? where?

if ($form['#type'] == 'checkboxes') {
   $getslice = array($getslice);
}

Documentation for checkboxes should be added to USAGE.txt

malcomio’s picture

Status: Needs work » Needs review
StatusFileSize
new659 bytes

Here is a patch of usage.txt which says how to target node reference checkboxes

jbrauer’s picture

Issue summary: View changes
Status: Needs review » Closed (outdated)

Closing D6 issues as it's no longer supported.