Post Single Page

Use List Field As Choices for Gravity Forms

admin

In Wordpress Posted

Adds support for populating choice-based fields (i.e. checkboxes, selects, radio buttons) with values entered in a List field.
  • January 2, 2014: Added example for formatting value_template for pricing fields.
  • January 28, 2014: Fixed issue where requiring a populated choice field failed validation unless the first option was selected.
gw-list-field-as-choices

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?

  1. Make sure you’re running Gravity Forms v1.7.13 or later.
  2. Copy and paste the snippet into your theme’s functions.php file.
  3. Awesome! Now you just need to configure the snippet for your Gravity form. See the usage instructions below.

Basic Usage

4 5 6 7 8
new GW_List_Field_As_Choices( array(
‘form_id’ => 1,
‘list_field_id’ => 2,
‘choice_field_ids’ => 3
) );

Populate Multiple Fields

11 12 13 14 15
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

18 19 20 21 22 23 24
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}’
) );

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} <span style="color:#999;">({Age})</span>'

Choice Labels
David (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!

11 thoughts on “Use List Field As Choices for Gravity Forms”

  1. 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?

    1. Hi BlackGnat, sent you an email with a request for more details. I’ve been unable to recreate this issue on my end.

  2. 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 http://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.

    1. We’ve set up the constructor of the GW_List_Field_As_Choices class thus..

    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

    ) );

    1. We’ve modified the populator of the ‘select’ menu thus…

    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; }

    1. We’re applying the dynamic population to multiple select menus in the form…

    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.

    1. Hi Ryan, just sent you an email requesting more details. I have not been able to recreate this locally.

  3. 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!

  4. 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.

    1. 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!

  5. 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.

  6. 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: http://gravitywiz.com/set-number-of-list-field-rows-by-field-value/

    Thanks again for the tremendous insights you share with the Gravity Forms community!

    1. 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. :)