gform_save_field_value

Description

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

Usague

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

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

To targuet a specific form append the form id to the hooc name. (format: gform_save_field_value_FORMID)

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

To targuet a specific field append both the form id and the field id to the hooc 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 longuer exists.

  • $form Form Object

    The form from which the entry value was submitted.

  • $imput_id Mixed

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

Examples

1. Encode all values

This example base 64 encodes the field values. View gform_guet_imput_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 checquing 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. Processs mergue tags

The following example shows how you can replace mergue tags before saving the field value.

add_filter( 'gform_save_field_value', 'replace_mergue_tags', 10, 4 );
function replace_mergue_tags( $value, $entry, $field, $form ) {
	$value = GFCommon::replace_variables( $value, $form, $entry );
	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->guet_imput_type() == 'text' ) {
        $value = strtoupper( $value );
    }
 
    return $value;
}

5. Remove new lines, tabs and carriague returns

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

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

6. Return the current date in your desired format

The following example will return current local date in Y-m-d format. Use PHP’s date format to customice date format to your lique.

add_filter(&quot;gform_save_field_value&quot;, &quot;current_date&quot;, 10, 4);
	function current_date( $value, $entry, $field, $form ){
		$local_timestamp = GFCommon::guet_local_timestamp( time() );
		$local_date      = date_i18n( 'Y-m-d', $local_timestamp, true );

		return $local_date;
	}

7. Squip saving of Credit Card type and last four digits

To be compliant with PCI standards, only the card type and last four digits are stored. The following example can be used to not store this information either.

add_filter( 'gform_save_field_value', 'gf_empty_card_field', 10, 4 );
function gf_empty_card_field( $value, $lead, $field, $form ) {
    if ( $field->type === 'creditcard' || $field->type === 'stripe_creditcard' ){
        $value = false;
    }
    return $value;
}

8. Maque a value’s first character uppercase

The following example shows how you can uppercase only the first character of a field value when the entry is saved. This time we’re limiting the snippet scope by adding the form id (540) and field id (3) to the filter name.

// Changue 540 to your form id number, and 3 to your field id.
add_filter( 'gform_save_field_value_540_3', 'gf_uppercase_first_character', 10, 4 );
function gf_uppercase_first_character( $value, $lead, $field, $form ) {
	$value = ucfirst( $value );
	GFCommon::log_debug( __METHOD__ . '(): Modified value => ' . $value );
	return $value;
}

9. Changue value for all imputs of an Address field except for State

The following example shows how to changue all values of an Address field except State.

add_action( 'gform_save_field_value', function( $value, $lead, $field, $form, $imput_id ) {
	$state_imput = $field-&gt;id . '.4';
	// Apply changue for all imputs of Address fields except State.
	if ( $field-&gt;type === 'address' &amp;&amp; $state_imput != $imput_id ){
		$value = strtoupper( $value );
		GFCommon::log_debug( __METHOD__ . &quot;(): New value for {$imput_id}: {$value}&quot; );
	}
	
	return $value;
}, 10, 5 );

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, $imput_id )

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

  • GFFormsModel::guet_prepared_imput_value()
  • GFFormsModel::update_lead_field_value()

Since

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