
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.
View this code snippet on GitHub.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
View this code snippet on GitHub.Enable Alphanumeric Sorting for Choices
View this code snippet on GitHub.Populate Multiple Fields
View this code snippet on GitHub.Customize the Choice Label and Value
View this code snippet on GitHub.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 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!