The checkbox field is surprisingly complex and we unfortunately don't have all the hooks in place to enable it to be populated dynamically without any limitations. The following code snippet is the best I could come up with. It will allow the checkboxes to be added to your form and you will be able to view the entry in the admin detail screen, but you won't be able to change the entry and update the checkbox values. You also won't be able to view the checkbox values in the entry list.
It is by no means a perfect solution, but like I said, it is the best we can do right now and it might be good enough for you.
I am going to keep this in mind and try to add the necessary hooks in the next versions so that checkboxes can be populated dynamically.
//Adds a filter to form id 26. Replace 26 with your actual form id
add_filter("gform_pre_render_26", populate_checkbox);
add_filter("gform_admin_pre_render_26", populate_checkbox);
function populate_checkbox($form){
//Creating choices
$choices = array(
array("text" => "Test 1", "value" => "test1"),
array("text" => "Test 2", "value" => "test2"),
array("text" => "Test 3", "value" => "test3")
);
$inputs = array(
array("label" => "Test 1", "id" => 2.1), //replace 2 in 2.1 with your field id
array("label" => "Test 2", "id" => 2.2), //replace 2 in 2.2 with your field id
array("label" => "Test 2", "id" => 2.3), //replace 2 in 2.3 with your field id
);
//Adding items to field id 2. Replace 2 with your actual field id. You can get the field id by looking at the input name in the markup.
foreach($form["fields"] as &$field){
//replace 2 with your checkbox field id
if($field["id"] == 2){
$field["choices"] = $choices;
$field["inputs"] = $inputs;
}
}
return $form;
}
Posted 14 years ago on Monday September 6, 2010 |
Permalink