gform_pre_submission

Description

This 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

Examples

1. Populate a field

This example changes the post variable for field 14:

add_action( 'gform_pre_submission', 'pre_submission_handler' );
function pre_submission_handler( $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', 'pre_submission_handler' );
	function pre_submission_handler( $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

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 should be placed in the functions.php file of your active theme.

Source Code

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