Have you ever needed to collect a personal phone number and an emergency phone number? Or maybe you need to collect a minor’s email address and also their parent/guardian’s email address? It’s important for these values to be unique. You don’t want users entering the same phone number or email address for both fields.
This snippet allows you to require the user to enter a unique value in each of the designated fields. You can specify multiple “groups” of fields
Getting Started
Check requirements
- Make sure you have Gravity Forms installed and activated.
- Already have a license? Download Latest Gravity Forms
- Need a license? Buy Gravity Forms
- Make sure you have Gravity Forms installed and activated.
Install the snippet
- Copy and paste the entire snippet into your theme’s functions.php file.
Configure the snippet
- Enter your form ID for the
form_id
parameter. - Enter each field ID that should be unique for the
field_ids
parameter. These field IDs should be passed as an array. - For more details on available parameters, see below.
- Enter your form ID for the
Usage Examples
Add a Custom Validation Message
View this code snippet on GitHub.Create Multiple Unique “Groups” on the Same Form
View this code snippet on GitHub.On some forms you may have multiple groups of fields that should be unique (i.e. two sets of phone number fields and two sets of email fields). You can create multiple instances of this snippet to create multiple unique groups.
Require Unique Field Compared Against ALL Form Fields
View this code snippet on GitHub.In some cases, you may want a field on the form to be completely unique from any other field on the form. Rather than specifying all field IDs, you can simply specify the single field ID that should be unique.
Parameters
form_id (int) (required)
The form ID of your form.
field_ids (array) (required)
An array of fields IDs that should be unique.
validation_message (string) (optional)
The message that is displayed if the field values are not unique. Defaults to:
'Please enter a unique value.'
.validate_all_fields (bool) (optional)
By default, the first field ID in the “field_ids” array is used as the “master” value and not validated. Enable this option to validate the the first field ID as well. Defaults to
false
.
How’d we do?
We’d love to hear what you think of this snippet. Did you run into any issues installing it? Let us know in the comments below.