August 31, 2022: Added
gwlfac_list_field_values
filter to allow filtering the List field values that will be used to populate choices. Improves compatibility with Gravity Flow!February 7, 2022: Migrated to Snippet Library.
June 24, 2014: Added support for a "sort" parameter which will sort the choices alphanumerically based on the "text" value.
January 28, 2014: Fixed issue where requiring a populated choice field failed validation unless the first option was selected.
January 2, 2014: Added example for formatting
value_template
for pricing fields.

I always have a hard time coming up with titles for snippets where the use case for something is not immediately obvious. Here’s a great use case that will help you understand the power of this handy snippet.
Let’s say you have a group activity sign-up form. You offer a variety of activities for groups but you need to know which members of the group will be participating in which activities.
This snippet can help you accomplish this by collecting the group members’ names in a List field and then populating those names in a checkbox field (or any other choice-based field) designated for each activity.
How do I get started?
- Make sure you’re running Gravity Forms v1.7.13 or later.
- Already have a license? Download Latest Gravity Forms
- Need a license? Buy Gravity Forms
- Copy and paste the snippet into your theme’s functions.php file.
- Awesome! Now you just need to configure the snippet for your Gravity form. See the usage instructions below.
Basic Usage
new GW_List_Field_As_Choices( array(
'form_id' => 1,
'list_field_id' => 2,
'choice_field_ids' => 3
) );
Enable Alphanumeric Sorting for Choices
new GW_List_Field_As_Choices( array(
'form_id' => 1,
'list_field_id' => 2,
'choice_field_ids' => 3,
'sort' => true
) );
Populate Multiple Fields
new GW_List_Field_As_Choices( array(
'form_id' => 384,
'list_field_id' => 3,
'choice_field_ids' => array( 6, 7 )
) );
Customize the Choice Label and Value
new GW_List_Field_As_Choices( array(
'form_id' => 384,
'list_field_id' => 2,
'choice_field_ids' => array( 4, 5 ),
'label_template' => '{Name} <span style="color:#999;font-style:italic;">({Age})</span>',
'value_template' => '{Name}'
) );
Auto List Field auto-syncs the number of rows in a List field with a Number or Quantity field value. It can also count the number of List field rows and use that value in Gravity Forms List field calculations.
This allows you to automatically add or subtract new List field rows based on the number value in another field!
Parameters
The form_id
, list_field_id
, and choice_field_ids
parameters are required. The snippet will fail silently; no error message is displayed.
- form_id (integer) (required) The form ID of your form.
- list_field_id (integer) (required) The field ID of the List field whose submitted values will be used to populate the choice fields.
- choice_field_ids (integer|array) (required) The field ID or fields IDs of the choice-based field(s) that will be populated from the user-entered List field values.
- label_template (string) (optional) Template used to determine how the user-entered data from the List field is used to generate the label of each choice in the choice field. Defaults to ‘{0}’. See Label Template below for more information on this.
- value_template (string) (optional) Description. Defaults to value of
label_template
parameter. See Value Template below for more information on this.
Label Template
Let’s say our List field has two columns: “Name” and “Age”. Now we’ll imagine the user has entered the following data into the list field:
David|26
Laura|27
Lee|19
The default generated choices would be:
David
Laura
Lee
Using the label_template
parameter, we can add additional user-entered data to the label. Simply use the name of the List field column to indicate which data you would like to display.
Template
'{Name}, {Age}'
Choice Labels
David, 26
Laura, 27
Lee, 19
You can also add formatting to further style and customize the choice label.
Template'{Name} ({Age})'
Choice LabelsDavid (26)
Laura (27)
Lee (19)
Value Template
The value of the choice field will default to the label unless you provide a value_template
. This parameter works in the same way as the label_template
. Having separate settings for the label and value templates allows you to have a choice label like “David (26)
” with a value like “David
“.
Value Template for Pricing Fields
If you want to populate a choice-based pricing field, you will need to format the value_template
like so:
value|price
Here is an example with actual values:
{Name}|5.25
Summary
What do you think? This is a pretty cool snippet, huh? As always, if you use it and like it, let me know!
Did this resource help you do something awesome with Gravity Forms?
Then you'll absolutely love Gravity Perks; a suite of 47+ essential add-ons for Gravity Forms with support you can count on.
Hi David, any chance to populate a select field (with enhanced interface) on another form with those values? List values from field #3 in form id 10 shall populate select field #17 in form 21. That would be very nice! Flo
Hi Flo, this might be possible by passing the data from the List field via the URL to the second form. You could then populate the data from the URL into a List field on the second form (optionally hidden via the gf_hidden class). Then you could use this snippet to populate the select on the second form.
Hi David,
This is an awesome snippet, lots of usecases for this one.
One question though, which I cannot seem to wrap my own brain around answering.
Is it possible to “reverse” the output on page 2 of the form, so that for example “David | 26” becomes to title and “Ski Lessons: Who is attending?” becomes the choice?
Imagine that it instead said “Ski lesson 1”, or “Advanced ski lesson” or something else that would make more sense.
Thanks in advance.
Hi Lars, not sure I understand. Could you clarify? Any visuals would help. :)
Hi.
I’d like to know if I want to make a simple list field like that. By default showing 1 field, then if user click ‘+’ it will add another line.
But I don’t want to use this as choices.
Thanks.
Hi Adlan, this is possible with Gravity Forms “List” field, available under the “Advanced Fields” field group.
Great stuff. What is this “rgar” function? I’m having a hard time looking it up. Is it documented somewhere?
Hi David Smith , can you tell me please how you added the button Add another row / Remove this row on demo page? Search this feature for a long time to use with gravity form.
Hi Cosmin, this is available Gravity Forms’ List field.
That’s great! very good work, I’m trying to do quite the same but I’m able I would like to to divide the process in two step. At the beginning of the year I use a form to insert a list of classroom: 1A, 2C, 5E, 3B and so on During the year I need to use it a lot of times in a lot of different form Can you please help me in this? Thank you very much in advance :)
Hi playmo, this is not currently possible with this snippet and would require a decent bit of customization to implement.
Excellent solution. Thank you! Is there a way to set the values of ‘form_id’, ‘list_field_id’, and ‘input_html_id’ to a variable so we do not have to enter a new array in functions.php for each form? Thanks!
Not currently. If this functionality proves popular enough it will be created as a perk with a UI so you could activate from the form editor. If this is something you need and would like to sponsor the development, please do get in touch.
Inspired by your work I am trying to do it in a reverse way. From a dropdown choice to text field. I try to get the dropdown value with
$drop_field = GFFormsModel::get_field( $form, 2 ); $idtest = GFFormsModel::get_field_value( $drop_field );
But with no success. Could you please tell me how do I fetch the dropdown choice? Thanks in advance.
thanks for all your information and education on gravity forms! i have just started using these on my site and am amazed at how powerful this tool is!
i had one quick question about sorting, in this example how would your sort this by age, and then by name?
i am working on a site with a large database of names and need an easier way to sort for users to find their name, but am having issues.
thanks so much!
Hi webdev, I’ve just updated the snippet to support a new “sort” parameter which will sort the choices alphanumerically based on the “text” value. A usage example has been added to the article above.
Hi great snippet of code which we have working up to the point of gathering the data once the form’s submitted. We have changed the code to populate a select instead of a checkbox. The select gets populated ok but the selected value does not get sent to the server. Is there something obvious to edit in the code for the select menu value to be sent?
Hi BlackGnat, sent you an email with a request for more details. I’ve been unable to recreate this issue on my end.
Hi,
Can you help me with this? I am having problems with the select menus…
We’ve checked the docs for implementing the dynamic population of a select menu at https://gravitywiz.com/use-list-field-choices-gravity-forms/. We can get a select menu populated ok but the value doesn’t get sent as part of the $entry.
function __construct( $args ) { $this->_args = wp_parse_args( $args, array( ‘form_id’ => false, ‘list_field_id’ => false, ‘choice_field_ids’ => false, ‘label_template’ => ‘{Name}’, //corresponding to field label name ‘value_template’ => ‘{Name}’ //redundant as label is default value
) );
if( GFFormsModel::get_input_type( $field ) == ‘select’ ) { $inputs = array(); foreach( $choices as $index => $choice ) { $inputs[] = array( ‘label’ => $choice[‘text’], ‘id’ => $field[‘id’] . ‘.’ . ( $index + 1 ) ); } $field[‘inputs’] = $inputs; }
new GW_List_Field_As_Choices( array( ‘form_id’ => 15, ‘list_field_id’ => 213, ‘choice_field_ids’ => array(225,243,252) ) );
All the select menus are populated but they do not send a value when the form is submitted. Have looked and looked but cannot see a reason. We could supply whole code base if necessary but sure it can only be a small issue that we cannot see.
Hi Ryan, just sent you an email requesting more details. I have not been able to recreate this locally.
Hi David,
Do you have any specifics on how to set up the form fields in the GF admin? I’m tyring to get the code to work and have not been successful. Do I need to check the “Populate Dynamically” checkbox in the admin?
Hi David,
I’m trying to implement the code and something’s not working correctly. Do you have any specifics on how the form needs to be set up in Gravity Forms? Does the “Populate Dynamically” option need to be checked on the choice field?
Thanks in advance.
Hi David,
The form export isn’t showing the dynamic population- there’s just many, many blank columns. How can we have their selection show up? Blank columns kind of defeat the purpose in economizing our registration process with dynamic population.
Thanks, Ashley
Forget I asked about multi-page forms. Looks like it was an incomplete HTML tag which took a long time to find, but has nothing to do with the snippet. Sorry to bug you!
Hi! I love your snippets. They have been a tremendous help to me.
I was wondering if there was a way to combine two different lists to be shown in one multiple choice field? I see that you can put the list into two multiple choice fields, but can you do it the other way around? Making the list_field_id an array.
Hi Stephen, it is possible but would require a modification of the “populate_choice_fields” method. Currently it only looks for a single list field, gets the values for that field and populates it. You would need to update this to loop through the array of list fields and get all the values for each field.
If you’d like to commission me to make this modification, get in touch!
I love this snippet. This is what I am looking for, but I was wondering if I have to have multiple pages to the form to populate the checkbox fields or can the checkbox fields be further down the form and it automatically fills when I get to it on the form.
Is there a way to have snippet to populate the checkbox field that is further down the page? Or does the form have to have a multiple page setup?
Many thanks for all your efforts here. You are a great talent.
I was planning to use this like a mini shopping cart. The action plan is; I get my customer to list the items they want to order (the list field auto populate using jquery as customer types in) the auto populate function would also insert a price in one of the rows and customer just adds the quantity required.
Your code, or a modified version of it, would create products and prices automatically for payment purposes.
Could this be possible or not?
Sample Scenario A customer has a prescription to fill and this prescription has multiple medications. For this person to work out the cost of this prescription he would need to list each item along with the quantity required.
Then the form would workout the total cost then charge the patient.
How would the price of the item be determined? Feel free to contact me to discuss this in more detail.
This is a great solution! Thanks!
I needed something almost exactly like this. The only issue I came across with using this was that if you’re charging for attendance to additional events/seminars, the product choices don’t appear to add up per checkbox. Not really an issue with this script, moreso with how Gravity Forms operates. If there was a way to add the options together that would be great – ie
Event name (Product field) John Doe | ($30.00) Jane Doe | ($30.00) Hank Hill | ($30.00)
Then total it up to $90.00
In the mean time I think the best solution for my scenario is to just have the user re-type names attending events by adding an extra Quantity field + List combo, described in: https://gravitywiz.com/set-number-of-list-field-rows-by-field-value/
Thanks again for the tremendous insights you share with the Gravity Forms community!
Hi Corbin, I actually think we can pull this off. The trick is to use an Option field (instead of Product field since Checkbox input type is not supported).
Then you’ll need to use a value template like this: “{Name}|30” changing “30” to whatever price each attendee should cost and “{Name}” to whatever the label of the desired column to display is (use “{0}” if you only have a single column on your List field). Here’s what my config looks like.
That should give you something like this. Let me know if you have any questions on getting this setup. :)