1

Trying to create new fields for my form dynamically, as I'm getting json from 3rd party API. Based on this json, I need a number of fields added to my form - not fixed number. So, I'm doing this, hooking it onto gform_pre_render:

add_filter( 'gform_pre_process', 'create_products_gforms' );
add_filter( 'gform_admin_pre_render', 'create_products_gforms' );
add_filter( 'gform_pre_render', 'create_products_gforms' );
function create_products_gforms( $form ) {

    $helper = new NSHelper();
    $state_name = $_POST['input_7'] ?? '';

    $code_value = $helper->get_state_code_by_name( $state_name );

    // Only fetch products if current form id is 33, state code is defined
    // and if there are products for this state.

    if ( $form['id'] != 33 || !$code_value ) {
       return $form;
    }

    // Get product list from NetSuit API based on state code
    $products_json_data = get_products_data( $code_value );

    $products = json_decode( json_decode( $products_json_data ) );

    $new_field_id = 0;
    foreach( $form['fields'] as $field ) {
        if( $field->id > $new_field_id ) {
            $new_field_id = $field->id;
        }
    }

    $new_field_id++;

    foreach ( $products as $product_object ) {
        // Prepare field properties
        $props = array(
            'id'        => $new_field_id,
            'type'      => 'singleproduct',
            'label'     => $product_object->onlinedisplayname,
            'basePrice' => floatval( $product_object->price ),
            'enableCalculation' => true
        );

        // Create new gravity forms field and add it to the form object
        $nf = GF_Fields::create( $props );

        // Hack - insert into array at specific position
        // Needed to display product fields before other fields
        // in the form
        array_splice( $form['fields'], 11, 0, array($nf) );

        $new_field_id++;
    }

    GFAPI::update_form( $form );

    $form['dynamic_fields_ids'] = $added_fields_ids;

    return $form;
}

This works, ie, it shows fields properly on the frontend. Now, the issue with this is that, once form is submitted, all the fields except these dynamically added ones are in the submission. But these are not. I assumed this has to do that these fields aren't registered in the form, so I also tried GFAPI::update_form( $form );, but that didn't help with submission part, though it udpates my form with new fields in the backend too.

Any ideas?

1
  • One thing to note is that fields temporarily added to the form (not saved in the db) should also set the formId property at creation. Adding the field to the $form['fields'] array is not enough. Without it, GF cannot 'see' the submitted data even though it exists in $_POST. Commented Jun 25, 2021 at 2:41

1 Answer 1

2

Based on your use-case, Milos, I would suggest using the gform_form_post_get_meta filter:

https://docs.gravityforms.com/gform_form_post_get_meta/

This will fire every time the form is fetched from the database and the most sure-fire way of guaranteeing your fields will be present.

If you prefer to be more surgical and stick with the gform_pre_render approach, you'll want to apply the same function on a couple other filters:

gform_pre_process
gform_admin_pre_render

Sign up to request clarification or add additional context in comments.

8 Comments

Thanks for trying. I've updated code in my question., as the code still doesn't update entry for some reason. However, I do get following for my newly created fields: Notice: Undefined index: 47.3 in /public/wp-content/plugins/gravityforms/includes/fields/class-gf-field-singleproduct.php on line 66
Also, please note that I don't really want to save new fields for the form, to use it another time. I want to create fields, put it in the form just for the sake of this current entry being created, then remove them from the form. So, I don't need permanent fields in the form, just temporary ones.
How are you planning on viewing this entry data? Without the fields in place, GF won't know what to show in the admin.
What do you mean? If there's no field in the admin, it won't show in the entries list? That sounds ridiculous, to be honest. I was planning on creating fields, update form data, update form in the backend, save entry, remove newly added fields from the backend.
also, regardless of that, as I said, code above still doesn't work, even though I have added GFAPI::update_form( $form );.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.