Pre Populated Checkbox fields not showing in entries and mails [RESVOLVED]

Hi, I am populating several checkboxes with values from ACF fields. That works great. However, when the form is submitted all those fields are not saved in entries. Here is my code in functions.php

add_filter( 'gform_pre_render_3', 'populate_checkbox' );
add_filter( 'gform_pre_validation_3', 'populate_checkbox' );
add_filter( 'gform_pre_submission_filter_3', 'populate_checkbox' );
add_filter( 'gform_admin_pre_render_3', 'populate_checkbox' );
function populate_checkbox( $form ) {
 
    foreach( $form['fields'] as &$field )  {
        //NOTE: replace ids with checkbox field ids
        if ( $field->id != 3 && $field->id != 4 && $field->id != 5 ) {
            continue;
        }
    //get field object stuff from ACF
    if($field->id == 5) {
      // get Arbeitsbereiche
      $options = get_field_object('field_618532e1e099f');
    } elseif($field->id == 4) {
	  // get Fachbereiche
	  $options = get_field_object('field_6185332ae09a0');
    } else {
	  // get Qualifikation
      $options = get_field_object('field_61853633923f5');
    }
 
        $input_id = 1;
        foreach( $options['choices'] as $value => $label ) {
 
        //skipping index that are multiples of 10 (multiples of 10 create problems as the input IDs)
        if ( $input_id % 10 == 0 ) {
            $input_id++;
        }
 
        $choices[] = array( 'text' => $label, 'value' => $value );   
        $inputs[] = array( 'label' => $label, 'id' => "{$field_id}.{$input_id}" );
 
        $input_id++;
        }
         
      $field->choices = $choices;
      $field->inputs = $inputs;
 
    }
 
    return $form;
}

I suspect there is some issue with the IDs? The fields also do not show up in mails with {all_fields}.

The checkbox HTML looks like that in the form:

<fieldset id="field_3_5" class="gfield gfield--width-full field_sublabel_below field_description_below gfield_visibility_visible">
	<legend class="gfield_label gfield_label_before_complex">Arbeitsbereiche</legend>
	<div class="ginput_container ginput_container_checkbox">
		<div class="gfield_checkbox" id="input_3_5">
			<div class="gchoice gchoice_3_5_1"> 
				<input class="gfield-choice-input" name="input_5.1" type="checkbox" value="Kaufmännische Abwicklung" id="choice_3_5_1"> 
				<label for="choice_3_5_1" id="label_3_5_1">Kaufmännische Abwicklung</label>
			</div>
			<div class="gchoice gchoice_3_5_2"> 
				<input class="gfield-choice-input" name="input_5.2" type="checkbox" value="Technische Abwicklung" id="choice_3_5_2"> 
				<label for="choice_3_5_2" id="label_3_5_2">Technische Abwicklung</label>
			</div>
			<div class="gchoice gchoice_3_5_3"> 
				<input class="gfield-choice-input" name="input_5.3" type="checkbox" value="AuĂźendienst" id="choice_3_5_3"> 
				<label for="choice_3_5_3" id="label_3_5_3">AuĂźendienst</label>
			</div>
			<div class="gchoice gchoice_3_5_4"> 
				<input class="gfield-choice-input" name="input_5.4" type="checkbox" value="Produktion/Logistik" id="choice_3_5_4"> 
				<label for="choice_3_5_4" id="label_3_5_4">Produktion/Logistik</label>
			</div>
		</div>
	</div>
</fieldset>

I would be happy if anybody had an idea what is going wrong here :wink:

Thanks Sascha

Sascha, the snippet looks fine to me, it’s pretty much the example from the documentation but changing the source of the data.

I would recommend you to make sure your page is not being cached, as caching would prevent the dynamic population code from working.

You can also enable logging on the Forms > Settings page and then on the Forms > Settings > Logging page ensure that Gravity Forms Core and any add-ons are enabled and set to log all messages.

Then you can add custom logging statements to your code to ensure it’s running when expected and dump information in key parts of your code to investigate what could be the issue, check instructions here: Custom Logging Statements - Gravity Forms Documentation

My bad. There was a $field_id where it should have been $field->id

Works well now :wink:

1 Like