Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Gravity Wiz // Gravity Forms // Map Submitted Field Values to Another Field
<?php
/**
* Gravity Wiz // Gravity Forms // Map Submitted Field Values to Another Field
*
* Usage
*
* 1 - Enable "Allow field to be populated dynamically" option on field which should be populated.
* 2 - In the "Parameter Name" input, enter the merge tag (or merge tags) of the field whose value whould be populated into this field.
*
* Basic Fields
*
* To map a single input field (and most other non-multi-choice fields) enter: {Field Label:1}. It is useful to note that
* you do not actually need the field label portion of any merge tag. {:1} would work as well. Change the "1" to the ID of your field.
*
* Multi-input Fields (i.e. Name, Address, etc)
*
* To map the first and last name of a Name field to a single field, follow the steps above and enter {First Name:1.3} {Last Name:1.6}.
* In this example it is assumed that the name field ID is "1". The input IDs for the first and last name of this field will always be "3" and "6".
*
* # Uses
*
* - use merge tags as post tags
* - aggregate list of checked checkboxes
* - map multiple conditional fields to a single field so you can refer to the single field for the selected value
*
* @version 1.1
* @author David Smith <david@gravitywiz.com>
* @license GPL-2.0+
* @link http://gravitywiz.com/...
* @copyright 2014 Gravity Wiz
*/
class GWMapFieldToField {
public $lead = null;
function __construct( ) {
add_filter( 'gform_pre_validation', array( $this, 'map_field_to_field' ), 11 );
}
function map_field_to_field( $form ) {
foreach( $form['fields'] as $field ) {
if( is_array( $field['inputs'] ) ) {
$inputs = $field['inputs'];
} else {
$inputs = array(
array(
'id' => $field['id'],
'name' => $field['inputName']
)
);
}
foreach( $inputs as $input ) {
$value = rgar( $input, 'name' );
if( ! $value )
continue;
$post_key = 'input_' . str_replace( '.', '_', $input['id'] );
$current_value = rgpost( $post_key );
preg_match_all( '/{[^{]*?:(\d+(\.\d+)?)(:(.*?))?}/mi', $input['name'], $matches, PREG_SET_ORDER );
// if there is no merge tag in inputName - OR - if there is already a value populated for this field, don't overwrite
if( empty( $matches ) )
continue;
$entry = $this->get_lead( $form );
foreach( $matches as $match ) {
list( $tag, $field_id, $input_id, $filters, $filter ) = array_pad( $match, 5, 0 );
$force = $filter === 'force';
$tag_field = RGFormsModel::get_field( $form, $field_id );
// only process replacement if there is no value OR if force filter is provided
$process_replacement = ! $current_value || $force;
if( $process_replacement && ! RGFormsModel::is_field_hidden( $form, $tag_field, array() ) ) {
$field_value = GFCommon::replace_variables( $tag, $form, $entry );
if( is_array( $field_value ) ) {
$field_value = implode( ',', array_filter( $field_value ) );
}
} else {
$field_value = '';
}
$value = trim( str_replace( $match[0], $field_value, $value ) );
}
if( $value ) {
$_POST[$post_key] = $value;
}
}
}
return $form;
}
function get_lead( $form ) {
if( ! $this->lead )
$this->lead = GFFormsModel::create_lead( $form );
return $this->lead;
}
}
new GWMapFieldToField();
@nwtech

This comment has been minimized.

Copy link

nwtech commented Feb 26, 2014

This is an awesome addition! I do have a couple of questions, maybe you can help?

  1. I'm trying to populate a field based on a name field and a selection from a radio button. It works when I only use the name field, as soon as I add the radio button field, it returns blank.
  2. Do the fields get populated upon clicking submit? Reason I'm asking is I'm using two date fields that need to be filled prior to submitting, else it returns an error.
  3. Is there a way to alter the mapping? Such as mapping a number or date field to another number or date field, but adding 1 to that mapped number?
@end1dream

This comment has been minimized.

Copy link

end1dream commented Nov 26, 2014

Hi spivurno,

I pasted this code into Code Snippets, and it was activated successfully.

Unfortunately, I cannot get anything to work.

  1. I set-up a test two-page form in Gravity Forms.
  2. Each page has a single line text field.
  3. I enable "Allow field to be populated dynamically" in the field on page 2.
  4. As a parameter, I write {:1}.
  5. I save the form and test it. I write something in the text field on page 1.
  6. Nothing. I press next and move on to page 2. The field on page 2 is not populated with the text on page one.

Am I missing something?

Thanks,

D.

@jhonatang1988

This comment has been minimized.

Copy link

jhonatang1988 commented Jul 4, 2015

Hi, when the user makes a mistake submitting the form, then solve the problem and resubmit, the script stop working, What could be problem?

Thanks in advance

@millardsimon

This comment has been minimized.

Copy link

millardsimon commented Sep 1, 2016

Hi David,

Sorry to be a pain, using this alongside : https://gist.github.com/spivurno/4055158 if I enter a banned email which prevents progressing then this snippet picks up the first entry made, not the corrected valid email. I wonder if this is because of multi-page form: https://qlikacademicprogram.qlik.com/student-application-form/

If you happen to be able to help that would be great.
Simon

@espermedia

This comment has been minimized.

Copy link

espermedia commented Oct 5, 2016

Any update to allow this to work on form re-submission? Having same issue as jhonatang1988 - user validation fails, user fixes the issue and re-submits - script does not update...

@unlimitedit

This comment has been minimized.

Copy link

unlimitedit commented Dec 23, 2016

Thanks David! This is a very useful script that maps fields as described. Unfortunately my understanding of PHP and Gravity Forms isn't sufficient to allow me to manipulate it to deliver exactly what I'm looking for. Perhaps you can help?

EDIT I discovered the answer and have submitted it at end of this comment

The field that I'm mapping is a Drop Down populated by a CPT. When an option is chosen and the form submitted, GF displays the post ID for the primary field but the mapped field displays the post_name. For the purpose which it's being used (specifying the parent post in a WP-Types CPT relationship), I need to have the mapped field displaying the ID rather than the post_name. What changes do I need to make so that the script functions in this manner?

ANSWER It turns out that the functionality required to achieve the above can be found in the sas786 fork of this script https://gist.github.com/saas786/82db9587e5b9644cbcf8

@espermedia

This comment has been minimized.

Copy link

espermedia commented Jan 11, 2017

I'm trying to use this to store the Cardholder Name of a Credit Card field to a hidden admin-only field. The field is {Cardholder Name:19.5}, but nothing is returned. I tried using saas786's fork and was able to get the field to populate, but it is now storing all of the credit card sub-fields including the card number, Array (sub-field array of Card Month and Card Year select fields 19.2[]), card security code, and cardholder name. How can I modify this to only store the last item rather than the array for the entire field, please?

@fhervazquez

This comment has been minimized.

Copy link

fhervazquez commented Mar 23, 2017

BUG: In multi step forms, if for some reason yo go back and forth the data is not passed.

@corvusgrp

This comment has been minimized.

Copy link

corvusgrp commented Aug 29, 2017

2 questions:

  1. Does this script require Gravity Wiz?
  2. How do I install this?
@bkellaway

This comment has been minimized.

Copy link

bkellaway commented Aug 30, 2017

I tried this on a dynamically populated drop down. Code here --> https://www.gravityhelp.com/documentation/article/dynamically-populating-drop-down-fields/

When using this for a dynamically populated drop down the label is stored - not the value.

Any help ?

gv hidden entiry
gv hidden

@jorgept

This comment has been minimized.

Copy link

jorgept commented Sep 23, 2017

Hi,

where i insert the snippet?
in functions.php
Thanks

@pareshmithra

This comment has been minimized.

Copy link

pareshmithra commented Apr 25, 2018

Hi,

In multi page form, if user selects one wrong field and clicks on next page and comes back, then selects the correct field, then dynamic population shows the first value(i.e the wrong field). How to rectify?

@prunly

This comment has been minimized.

Copy link

prunly commented Sep 5, 2018

@bkellaway (or others who have the same question)

When using this for a dynamically populated drop down the label is stored - not the value.

Answer lies in the docs for merge tags here. Just use the following syntax:
{Field Name:Field ID:value}
For example:
{Choice:7:value}

@it40093

This comment has been minimized.

Copy link

it40093 commented Nov 13, 2018

Hi, if I update the source field on Admin side the target field is not updated accordingly. How can I maintain the values in sync when I update from Admin area? Many thanks

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.