gform_pre_submission

Description

The gform_pre_submission action hook is executed after form validation, but before any notifications are sent and the entry is stored. This action can be used to modify the posted values prior to creating the entry.

Usage

Applies to all forms.

add_action( 'gform_pre_submission', 'pre_submission' );

Applies to a specific form. In this case, form id 5.

add_action( 'gform_pre_submission_5', 'pre_submission' );

Parameters

ParameterTypeDescription
$formForm ObjectThe current form.

Examples

1. Populate a field

This example changes the post variable for field 14:

add_action( 'gform_pre_submission', 'populate_field_14' );
function populate_field_14( $form ) {
	$_POST['input_14'] = 'new value for field 14';
}

2. Populate a field using the value from another field

This example changes the post variable for field 14 to the value of field 5 for a form with id 1:

// Change 1 on the following to your form id number.
add_action( 'gform_pre_submission_1', 'populate_field_14_from_field_5' );
function populate_field_14_from_field_5( $form ) {
	// Change 14 and 5 to the id number of your fields.
	$_POST['input_14'] = rgpost( 'input_5' );
}

3. Populate a field with the age

This example calculates an age from a date field and populates another field with the result.

add_action( 'gform_pre_submission_5', function ( $form ) {
	// Get the date field.
	$date_field_id = '10';
	$date_field    = GFAPI::get_field( $form, $date_field_id );

	// Get the date field value.
	$value      = $date_field->get_value_submission( array() );
	$date_value = GFFormsModel::prepare_date( $date_field->dateFormat, $value );

	// Get the DateTime object representing the difference between the date field value and today.
	$today = new DateTime();
	$diff  = $today->diff( new DateTime( $date_value ) );

	// Populate field 11 with the age.
	$_POST['input_11'] = $diff->y;
} );

Placement

This code can be used in the functions.php file of the active theme, a custom functions plugin, a custom add-on, or with a code snippets plugin.

See also the PHP section in this article: Where Do I Put This Code?

Source Code

This action hook is located in GFFormDisplay::process_form() in form_display.php.