Using GFAPI::submit_form() to add entries

I am trying to use GFAPI::submit_form() to add entries to an assessment form (ID=2) with the following code within functions.php.

This code should fire when a user registers with the website via another user registration form (ID = 1) that has a User Registration Feed set up to create a user.

The code hooks into ‘gform_user_registered’ the Gravity Forms action to trigger an event once a user has been registered through the User Registration Add-on.

add_action( ‘gform_user_registered’, ‘gmpcc_register_update_user_profile’, 10, 4 );
function gmpcc_register_update_user_profile ($user_id, $feed, $entry, $user_pass) {

// Get values from the user registration form (ID = 1) with field IDs
$firstname = rgar( $entry, ‘2.3’ ); // First name
$lastname = rgar( $entry, ‘2.6’ ); // Last name
$email = rgar( $entry, ‘3’ ); // Email
$telephone = rgar( $entry, ‘36’ ); // Telephone
$jobtitle = rgar( $entry, ‘31’ ); // Job title
$organisation = rgar( $entry, ‘30’ ); // Organisation
$locality = rgar( $entry, ‘48’ ); // Locality
$locations_to_work = rgar( $entry, ‘50’ ); // Locations to work
$health_care_professional = rgar( $entry, ‘37’ ); // Health Care Professional

// Update user profile with custom fields
update_user_meta($user_id, ‘gmpcc_user_first_name’, $firstname);
update_user_meta($user_id, ‘gmpcc_user_last_name’, $lastname);
update_user_meta($user_id, ‘gmpcc_user_email’, $email);
update_user_meta($user_id, ‘gmpcc_user_telephone’, $telephone);
update_user_meta($user_id, ‘gmpcc_user_jobtitle’, $jobtitle);
update_user_meta($user_id, ‘gmpcc_user_organisation’, $organisation);
update_user_meta($user_id, ‘gmpcc_user_locality’, $locality);
update_user_meta($user_id, ‘gmpcc_user_locations_want_to_work’, $locations_to_work);
update_user_meta($user_id, ‘gmpcc_user_health_care_professional’, $health_care_professional);

// Add entry to assessment form (ID=2)
$input_values[‘input_1’] = $firstname; // Text field
$input_values[‘input_2’] = $lastname; // Text field
$input_values[‘input_4’] = $email; // Text field
$input_values[‘input_96’] = $telephone; // Text field
$input_values[‘input_18’] = $jobtitle; // Text field
$input_values[‘input_95’] = $organisation; // Text field
$input_values[‘input_145’] = $locality; // Select field
$input_values[‘input_168’] = $locations_to_work; // Multi-select field
$input_values[‘input_6’] = $health_care_professional; // Radio buttons field
$result = GFAPI::submit_form( 2, $input_values );

}

An entry is created in the user registration form (ID = 1) .
A user account is created.
Custom fields are added to user profile.
No entry is created in the assessment form (ID=2).

Please could you help me to troubleshoot how to get GFAPI::submit_form() to add entries.


Many thanks
Rowan

How strict is GFAPI::submit_form() in terms of validation?

There are required fields on the form that I have not included in my list of $input_values. Could this explain why the entries are not created in the assessment form (ID=2)?

Also is there anything I need to be aware of when submitting the values from different field input types, such as Select, Multi-select and Radio buttons?


Many thanks
Rowan

It works just like a standard form submission so it will validate the fields and if a field is required and a value wasn’t supplied it will return a validation error.

You can add a custom logging statement to check the $result:

GFCommon::log_debug( __METHOD__ . '(): result => ' . print_r( $result, true ) );

For choice based fields you need to pass the value of the selected choice.

The multi select field is a little different, it expects an array containing the values of the selected choices.

Hi Richard,

Thank you for your really helpful reply.

For the choice based fields such as $locality the value of the selected choice is stored so would the following work?

$input_values[‘input_145’] = $locality;

For the multi-select fields such as $locations_to_work the data is stored as follows

[“PCN”,“GMWB”,“MVS”,“GMHSCP”]

In order to pass this to the form as an array would the following work?

$locations_to_work = rgar( $entry, ‘50’ );
$locations_to_work_string = str_replace(array(’[’,’"’,’]’), ‘’, $locations_to_work);
$locations_to_work_array = explode(’,’, $locations_to_work_string);

If so I assume that this would enable me to use the following with $input_values

$input_values[‘input_168’] = $locations_to_work_array;


Many thanks
Rowan

The $locality line should be fine.

For the multi select you can json decode the entry value e.g.

$input_values[‘input_168’] = $locations_to_work ? json_decode( $locations_to_work ) : array();

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