December 5, 2022: Added support for requiring unique values in List field columns.
September 23, 2022: Added support for case-insensitive comparisons when evaluating whether values are unique.
September 14, 2021: Migrated snippet to the Snippet Library.
January 11, 2020: Added support for requiring unique filenames in File Upload fields (single files only).
November 18, 2019: Added support for experimental "mode" parameter. Defaults to "collective". Also accepts "individual" to require each value to be unique when validating a multi-value field.
December 10, 2018: Fixed issue when comparing values of Checkbox fields (and other multi-input field types).
October 28, 2017: Added support for new parameter "validate_all_fields". Will force the "master" field to be validated along with any other fields in the validation group.
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
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
- Enter each field ID that should be unique for the
field_idsparameter. These field IDs should be passed as an array.
- For more details on available parameters, see below.
- Enter your form ID for the
Add a Custom Validation Message
new GW_Require_Unique_Values( array( 'form_id' => 12, 'field_ids' => array( 14, 15 ), 'validation_message' => 'My custom validation message!' ) );
Create Multiple Unique “Groups” on the Same Form
new GW_Require_Unique_Values( array( 'form_id' => 2, 'field_ids' => array( 4, 5 ) ) ); new GW_Require_Unique_Values( array( 'form_id' => 2, 'field_ids' => array( 7, 8 ) ) );
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
new GW_Require_Unique_Values( array( 'form_id' => 2, 'field_ids' => array( 7 ) ) );
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.
Require Unique Values in List Field Columns
new GW_Require_Unique_Values( array( 'form_id' => 2, 'field_ids' => array( 9 ) ) );
If you specify a single field ID and that field is a List field, this snippet will automatically require that all columns contain unique values. For example, if two rows had the value “Bilbo Baggins” in the first column, a validation error would be returned.
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
case_sensitive (bool) (optional)
Indicate whether the unique value comparison should be case-sensitive. Defaults to
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.