Using Gravity Forms ‘gform_pre_submission’ PHP action

The gform_pre_submission Gravity Forms action hook allows you to modify the submitted form values after validation, but before the entry is stored and notifications are sent.

Table of contents

Usage

Apply to all forms:

add_action('gform_pre_submission', 'pre_submission');

Apply to a specific form (e.g., form ID 5):

add_action('gform_pre_submission_5', 'pre_submission');

Parameters

  • $form (Form Object): The current form.

More information

See Gravity Forms Docs: gform_pre_submission

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

Examples

Populate a field

This example sets a new value for field 14:

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

Populate a field using the value from another field

This example sets the value of field 14 to the value of field 5 for form ID 1:

add_action('gform_pre_submission_1', 'pre_submission_handler');
function pre_submission_handler($form) {
    $_POST['input_14'] = rgpost('input_5');
}

Populate a field with the age

This example calculates the age based on the date field value and populates field 11 with the age for form ID 5:

add_action('gform_pre_submission_5', function($form) {
    $date_field_id = '10';
    $date_field = GFAPI::get_field($form, $date_field_id);

    $value = $date_field->get_value_submission(array());
    $date_value = GFFormsModel::prepare_date($date_field->dateFormat, $value);

    $today = new DateTime();
    $diff = $today->diff(new DateTime($date_value));

    $_POST['input_11'] = $diff->y;
});

 

Leave a Comment

Your email address will not be published. Required fields are marked *