bookmark_bordergform_save_field_value

Description

Use this filter to change the field’s value before saving it to the database. Use in conjunction with gform_get_input_value to perform low level transformations, such as encrypting/decrypting a field.

Usage

The base filter which would run for all forms and all fields would be used like so:

add_filter( 'gform_save_field_value', 'your_function_name', 10, 5 );

To target a specific form append the form id to the hook name. (format: gform_save_field_value_FORMID)

add_filter( 'gform_save_field_value_10', 'your_function_name', 10, 5 );

To target a specific field append both the form id and the field id to the hook name. (format: gform_save_field_valuee_FORMID_FIELDID)

add_filter( 'gform_save_field_value_10_3', 'your_function_name', 10, 5 );

Parameters

  • $value string

    The current entry value to be filtered.

  • $entry Entry Object

    The current entry.

  • $field Field Object | null

    The field from which the entry value was submitted or null if updating the entry and the field no longer exists.

  • $form Form Object

    The form from which the entry value was submitted.

  • $input_id mixed

    The input ID of the input being saved. Defaults to the field ID for single input field types. Added in GF v1.8.5.8

Examples

1. Encode all values

This example base 64 encodes the field values. View gform_get_input_value for an example on how to decode the fields.

add_filter( 'gform_save_field_value', 'save_field_value', 10, 4 );
function save_field_value( $value, $lead, $field, $form ) {
    return base64_encode( $value );
}

This example uses the GFCommon::encrypt() method.

add_filter( 'gform_save_field_value', 'save_field_value', 10, 4 );
function save_field_value( $value, $lead, $field, $form ) {
    return GFCommon::encrypt( $value );
}

2. Encode values for a specific form

This is another example where you may select a specific form and specific fields to encode.

add_filter( 'gform_save_field_value', 'save_field_value', 10, 4 );
function save_field_value( $value, $lead, $field, $form ) {
        //if not the form with fields to encode, just return the unaltered value without checking the fields
        if ( ! is_object( $field ) || absint( $form->id ) <> 94 ) {
                return $value;
        }

        //array of field ids to encode
        $encode_fields = array( 1, 2, 3 );

        //see if the current field id is in the array of fields to encode; encode if so, otherwise return unaltered value
        if ( in_array( $field->id, $encode_fields ) ) {
                return base64_encode( $value );
        } else {
                return $value;
        }
}

3. Process merge tag

The following example shows how you can replace the entry id merge tag when the entry is saved.

add_filter( 'gform_save_field_value', 'replace_entry_id', 10, 2 );
function replace_entry_id( $value, $entry ) {
    $value = str_replace( '{entry_id}', rgar( $entry, 'id' ), $value );

    return $value;
}

4. Uppercase Value

The following example shows how you can uppercase a field value when the entry is saved.

add_filter( 'gform_save_field_value', 'uppercase_text', 10, 3 );
function uppercase_text( $value, $entry, $field ) {
    if ( $field->get_input_type() == 'text' ) {
        $value = strtoupper( $value );
    }

    return $value;
}

5. Remove new lines, tabs and carriage returns

The following example shows how you can remove new lines, tabs and carriage returns before saving the field value.

add_filter("gform_save_field_value", "remove_n_t_r", 10, 4);
function remove_n_t_r($value, $entry, $field, $form){
	$new_value = str_replace(array("\n", "\t", "\r"), '', $value);
	return $new_value;
}

Placement

This code should be placed in the functions.php file of your active theme.

Source Code

gf_apply_filters( 'gform_save_field_value', array( $form['id'], $field->id ), $value, $lead, $field, $form, $input_id )

This filter is located in the following methods in forms_model.php

  • GFFormsModel::get_prepared_input_value()
  • GFFormsModel::update_lead_field_value()

Since

The base filter was added in Gravity Forms 1.5. The form and field specific versions were added in Gravity Forms 1.9.13.6