Change state of checkbox to isSelected = true

I am trying to set the state of a specific checkbox in a form to selected based on it’s value matching a string, all the below does is return three arrays (the number of choices in this checkbox field)

// https://docs.gravityforms.com/gf_field_checkbox/
// https://docs.gravityforms.com/gform_field_choice_markup_pre_render/
// https://community.gravityforms.com/t/ive-set-isselected-1-on-form-render-but-this-is-not-showing-on-the-form-resolved/13291
// https://stackoverflow.com/questions/60851099/gravity-forms-pre-selected-choices-checkboxes-field
// https://www.gravityforms.com/blog/dynamic-population-tutorial/

add_filter( 'gform_field_choice_markup_pre_render_3_7', function ( $choice_markup, $choice, $field, $value ) {

foreach( $form['fields'] as &$field ) {
   if($field->id == 7) {
      $options = $field["choices"];
      foreach ( $options as $fkey => $option ) {
         if( $option['value'] == 'Biotechnology'){
           $choices[] = array( 'text' => $option['text'], 'value' => $option['value'], 'isSelected' => true );
         } else {
           $choices[] = array( 'text' => $option['text'], 'value' => $option['value'], 'isSelected' => false );
         }
      }
      $field->choices = $choices;
   }
}
	return $form;

}, 10, 4 ); 

Another way of doing this woule be to populate form A with an existing entry for form A. But the link below doesn’t talk about populating the fields with what the user entered previously

@essexboyracer You are using code that is not matched to the filter you’ve chosen. The gform_field_choice_markup_pre_render filter is filtering the html markup for the field, contained in the first variable $choice_markup. So your function is incorrectly return the $form object in place of the HTML markup.

Filters in Wordpress may pass on more than 1 variable parameters, but it’s all about the first parameter.

Your code will work in an earlier filter (before markup stuff), you can replace your first line with this:

add_filter( 'gform_pre_render_3', function ( $form ) {

Also remove the , 4 on your last line, and it should work as you intend.

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.