Hi Chris,
I have found out that checkbox items added dynamically on "gform_pre_render" / "gform_admin_pre_render" are not available to functions added to form submission hooks (such as "gform_validation", "gform_pre_submission" etc), unless the form is saved in the GF admin area after dynamic population.
When pure arbitrary data is used to pre-render the checkboxes, the above workaround is not possible.
Therefore I use the following approach which seems to work very well for me: on "gform_pre_render" I use RGFormsModel::get_form_meta() method to fetch the form from database, then populate the form controls as required, and then the form is saved back to the database using RGFormsModel::update_form_meta(). This behaviour can be controlled using a boolean constant, as shown below:
// When true, the form will be saved to DB after dynamic population
define("SAVE_FORM_ON_PRE_RENDER", true);
// 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) {
if (SAVE_FORM_ON_PRE_RENDER)
$the_form = RGFormsModel::get_form_meta($form['id']);
else
$the_form = &$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 ($the_form["fields"] as &$field) {
// replace 2 with your checkbox field id
if ($field["id"] == 2) {
$field["choices"] = $choices;
$field["inputs"] = $inputs;
}
}
// Save form to database, if constant is set to true
if (SAVE_FORM_ON_PRE_RENDER)
RGFormsModel::update_form_meta($form['id'], $the_form);
return $the_form;
}
Using this approach you can view the checkbox values in the entry list as well as change the entry, by updating the submitted checkbox values.
Hope this helps.
Posted 12 years ago on Monday September 17, 2012 |
Permalink