Created
June 1, 2023 18:28
-
-
Save coulterpeterson/2e9a16a389857d5fb9d0f6dd14481d87 to your computer and use it in GitHub Desktop.
Gravity Forms | Check For Duplicate Values Between a Set of Fields
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Gravity Wiz // Gravity Forms // Check For Duplicate Values Between a Set of Fields | |
* https://gravitywiz.com/ | |
* | |
* Checks for duplicate values between a set of fields. Can be used to create a manual "rank these" setup | |
* using dropdown fields. | |
* | |
* Instructions: | |
* Update "123" to your form ID, "4" to the field ID of the last field that you want to check for duplicate values, | |
* and "5, 6, 7, 8" to the IDs of the rest of the fields you want to check for duplicate values. | |
* | |
*/ | |
add_filter( 'gform_field_validation_123_4', function( $result, $value, $form ) { | |
$other_field_ids = array( 5, 6, 7, 8 ); | |
$last_field = $value; | |
$field_values = array( $last_field ); | |
foreach( $other_field_ids as $field_id ) { | |
array_push( $field_values, rgpost( 'input_' . $field_id ) ); | |
} | |
$double_found = false; | |
foreach( $field_values as $field_value_x ) { | |
$doubles_count = 0; // If becomes more than 1 (finding itself), then an entry was submitted twice | |
foreach( $field_values as $field_value_y ) { | |
if( $field_value_x == $field_value_y ) { | |
$doubles_count++; | |
} | |
} | |
if( $doubles_count > 1 ) { | |
$double_found = true; | |
} | |
} | |
if ( $result['is_valid'] && $double_found ) { | |
$result['is_valid'] = false; | |
$result['message'] = 'Some options were ranked multiple times. Please try again.'; // Can edit this message to your preference. | |
} | |
return $result; | |
}, 10, 4 ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment