Create fields dynamically [RESOLVED]

Hi Everyone,

I have an issue with fields that are dynamically added to a form. Fields are added nicely, but the problem is that in the back-end I cannot see the values.

Here’s my code:

add_filter( 'gform_pre_render', 'handle_report_form' );
function handle_report_form($form) {
    if($form['id'] === 4) {
        $campaign_id = (isset($_GET['campaign_id'])) ? intval($_GET['campaign_id']) : false;
        $today = date('Y-m-d');
        $end_date = get_field('slutdatum', $campaign_id);

        if($end_date < $today) {
            return;
        }

        if($campaign_id) {
            $products = get_field('sortiment', $campaign_id);
            foreach ($products as $key => $product) {
                $product_name = get_the_title($product['sortimentprodukt']);
                $new_field_id = GFFormsModel::get_next_field_id( $form['fields'] );

                $props = array(
                    'id' => $new_field_id,
                    'label' => $product_name,
                    'inputName' => 'product_id_' . $key,
                    'defaultValue' => '0',
                    'type' => 'number'
                );
                $field = GF_Fields::create( $props );
                array_push( $form['fields'], $field );
            }
        }
    }
    return $form;
}

Hi Zoltan. Normally, when using gform_pre_render like that, you also need to use three other filters, to populate those fields in the other locations they are displayed. You may be able to add the filters like this:

add_filter( 'gform_pre_render', 'handle_report_form' );
 
// Note: when changing choice values, we also need to use the gform_pre_validation so that the new values are available when validating the field.
add_filter( 'gform_pre_validation', 'handle_report_form' );
 
// Note: when changing choice values, we also need to use the gform_admin_pre_render so that the right values are displayed when editing the entry.
add_filter( 'gform_admin_pre_render', 'handle_report_form' );
 
// Note: this will allow for the labels to be used during the submission process in case values are enabled
add_filter( 'gform_pre_submission_filter', 'handle_report_form' );

Documentation:

Or, instead of using four filters, you could change from gform_pre_render to gform_form_post_get_meta:

add_filter( 'gform_form_post_get_meta', 'handle_report_form' );

Documentation:

If you have any other questions, please let us know.

Thanks for all your help Chris, this is almost perfect! The only issue is that when after submitting the form for the first time all the fields get added inside the form back-end. Is this something that can be avoided?

gform_admin_pre_render is the filter that is doing that. What should happen instead of adding the fields to the form view in the editor?

Thank you Chris, after playing around with the gform_admin_pre_render filter a bit, I managed to achieve the desired outcome.

Thanks so much for your help, this ticket can be closed.