Hello,
I have a scenarrio. I have to pre-fill a form based on response from an API. I have filled text fields using defaultValue property but when it comes to pre-filling/pre-select checkbox options, it is not working.
Here is an example.
I have created a form with checkboxes which has choices : Red, Green, Blue, Yellow.
In plugin code, i am trying to do following:
$selected_choices = array("Red", "Yellow");
$choices = $field->choices;
$choices_updated = $choices;
foreach($choices_updated as &$choice){
foreach($selected_choices as $selected_choice)
if($selected_choice == $choice['value']){
$choice['isSelected'] = true
}
}
}
$field->choices = $choices_updated;
//If I print_r or var_dump $field here then it is showing updated values of $choice but on frontend, choices are showing un-checked.
add_filter( 'gform_pre_render_6', 'populate_after_aapp_id' );
add_filter( 'gform_pre_validation_6', 'populate_after_aapp_id' );
add_filter( 'gform_pre_submission_filter_6', 'populate_after_aapp_id' );
add_filter( 'gform_admin_pre_render_6', 'populate_after_aapp_id' );
function populate_after_aapp_id( $form ) {
// Do cURL request here to fetch values from API. I am excluding that part here.
// I have response from cURL similar to $selected_choices (for checkbox field only).
$selected_choices = array("Red", "Yellow");
if($field->inputName == 'Colors' ){
$choices = $field->choices;
$choices_updated = $choices;
foreach($choices_updated as &$choice){
foreach($selected_choices as $selected_choice)
if($selected_choice == $choice['value']){
$choice['isSelected'] = true
}
}
}
$field->choices = $choices_updated;
}
}
Hello @chrishajer
Thank you for your response. I have checked the example. i can change field value or label from above example. But i have issue with isSelected isSelected
Like I said earlier i want to show selected checkbox to user on page load. based on example you have shared, I can change field label or value and it is working fine where as isSelected is not working.
Following is the updated code example:
add_filter( 'gform_pre_render_6', 'populate_after_aapp_id' );
add_filter( 'gform_pre_validation_6', 'populate_after_aapp_id' );
add_filter( 'gform_pre_submission_filter_6', 'populate_after_aapp_id' );
add_filter( 'gform_admin_pre_render_6', 'populate_after_aapp_id' );
function populate_after_aapp_id( $form ) {
// Do cURL request here to fetch values from API. I am excluding that part here.
// I have response from cURL similar to $selected_choices (for checkbox field only).
$selected_choices = array("Red", "Yellow");
if($field->inputName == 'Colors' ){
$choices = $field->choices;
$choices_updated = $choices;
foreach($choices_updated as &$choice){
foreach($selected_choices as $selected_choice) {
if($selected_choice == $choice['value']){
$choice['text'] = 'Text_'.$selected_choice;
$choice['value'] = 'Value_'.$selected_choice;
$choice['isSelected'] = true;
}
}
}
$field->choices = $choices_updated;
}
return $form;
}
based on above code, i can see Text_Red and Text_Yellow in updated checkboxes.
Are you setting isSelected before the form is initially displayed? Once the $_POST is populated by the user clicking next or submit the plugin uses the contents of the $_POST to determine which choices are selected, not the isSelected property.
@richardw8k Yes, I am updating form in pre_render Hook which means i am updating form fields before displaying the form.
And this is a multi page gravity form. Here is a screenshot how it actually look like on frontend:
That field is on page three which means your code is running after the $_POST is populated so the choice isSelected property isn’t used, that property would only be used for fields on page one. The plugin is using the contents of the $_POST to determine which choices are selected so you’ll need to populate it instead e.g.
$_POST['input_1_1'] = 'the choice value';
You would replace 1_1 with your field and input numbers.
@richardw8k My issue is not related to populating checbox values. My issues is related to setting a checkbox checked, which is not happening. I can populate checkbox text or value but can’t set checkbox as checked using code.
As you said, field is on page three so isSelected will not work, I have moved the field to page 1 and still isSelected is not working.
Sorry for the confusion, populate was the wrong word. You need to set the input value in the $_POST, that is what is used to determine which choices are selected.
@richardw8k Thank you for quick response. I have tried $_POST but is not working either. I have used it like this $_POST['input_22_1']= 'Hello World';
Where 22 is field ID and 1 is 1st choice. Nothing has changed. Another thing, as far i know is $_POST is used to get values of any form or action that has triggrred/submitted in past. If i var_dump or print_r $_POST, i am getting values of fields that i have filled in step 1 & step 2. At Step 1, $_POST returns nothing. I will suggest you to kindly read entire thread from start. @chrishajer and I are on same point, where as you are dragging out of the topic.
@chrishajer
I though to replace checkbox field with a multiselect field so user can select multiple values and We have similar issue with Multiselect as well. I can change option value or text but i cannot pre-select any option in multi-select too. Another issue I found is that somehow, pre-render filter is triggering twice on multi-select field and changing field text twice. Here is updated code for multi-select scenario.
According to above code, multi-select field should have an option Updated_Swimwear which should be pre-selected. But somehow, it is becoming Updated_Updated_Swimwear and it is not pre-selected as well.
I think there is some kind of bug with gravity forms.
This isn’t a bug. Out of the box the gform_pre_render filter is only triggered once per form render.
If the form is embedded in a page multiple times that would explain the issue. Some page builders have been known to do this, they include the page content in the page twice, once for desktops and again for mobile devices.
Also, some add-ons have been known to trigger the filter in their own code which can also explain it running multiple times.
For these reasons we don’t recommend appending the existing choice text or value back to themselves.
@richardw8k I verified this by inspecting HTML and gravity form is not double embedded into webpage . I also tested same in Gravity form Preview mode. As, far as it is related to Addons, i will test this by disabling all addons. I am still looking for a solution on isSelected not working on both checkbox or multiselect field.
I need to show checkbox values checked if conditions are true, I can change field text and value but isSelected = true is not making checkbox checked.
The only way to select choices when the $_POST is not empty is to add the choice value for the selected input(s) to the $_POST.
$_POST['input_1_1'] = 'the value of choice 1'; $_POST['input_1_3'] = 'the value of choice 3';
The problem with that is that you risk re-selecting the choice(s) if the user has already de-selected them when they are paging forwards or backwards through the form.