rwkiii
rwkiii

Reputation: 5846

How to add Gravity Forms fields in gform_pre_render()

I have a multi page Gravity Form that presents a series of questions by way of a radio button.

Which of the following would you choose?

    o  Choice #1
    o  Choice #2

    +----------+   +----------+  
    |   BACK   |   |   NEXT   |  
    +----------+   +----------+  

Each question and choice set is configured using custom fields (Advanced Custom Fields). I'm able to iterate through all of these questions and choices just fine from within the gform_pre_render filter and now I want to create the needed Gravity Form fields on the fly.

Specifically, there will be a page field and radio button field for every question.

I really have tried just about every search criteria I can think of on Google and scoured through documentation on Gravity Help, but I just don't see an example of adding fields dynamically.

Can someone shine a light for me? :P

Upvotes: 3

Views: 5536

Answers (1)

Dave from Gravity Wiz
Dave from Gravity Wiz

Reputation: 2859

You can create a field with GFFields::create(). Here's a rough example (assumes you are within the gform_pre_render filter.

$props = array( 
    'id' => 123,
    'label' => 'My Field Label',
    'type' => 'text'
);
$field = GF_Fields::create( $props );
array_push( $form['fields'], $field );

There are probably more properties you will need to specify to get the field working. I would recommend using print_r() on an existing field to get an idea of all the properties available. You will also want to make sure your field IDs are unique.

Lastly, in order for data to be captured from these existing fields, you will probably want to also add your dynamic fields via the gform_pre_validation filter as well.

Upvotes: 7

Related Questions