Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion gravity-forms/gw-require-unique-values.php
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,7 @@ public function get_filtered_value( $field, $input_id = null ) {
}

// When using a field ID (not input ID) for multi-input fields, combine all subfield values into one for validation.
if ( ! $input_id && is_array( $field->inputs ) && is_array( $value ) ) {
if ( ! $input_id && is_array( $field->inputs ) && is_array( $value ) && $field->type != 'email' ) {
$all_field_ids = $this->get_all_field_ids();
if ( in_array( $field->id, $all_field_ids ) ) {
$combined_parts = array();
Expand All @@ -218,6 +218,7 @@ public function get_filtered_value( $field, $input_id = null ) {
}

$value = ! is_array( $value ) ? array( $value ) : $value;
$value = $field->type == 'email' ? array( $value[0] ) : $value;
$value = array_filter( $value );

Comment on lines 220 to 223
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue

Bug: array index 0 may be undefined for associative arrays; breaks Email uniqueness when confirmation is enabled

$value = $field->type == 'email' ? array( $value[0] ) : $value; assumes $value is numerically indexed. With Email + Confirmation, Gravity Forms returns an associative array keyed by input IDs (e.g., 3.1, 3.2). $value[0] will be undefined, yielding array(null) which is then filtered out, causing empty($value) (Line 71) and silently skipping validation. Result: duplicates may pass.

Fix by selecting the primary email sub-input ID from $field->inputs when validating by field ID (i.e., when $input_id is not provided), and falling back to the first element if needed. Also use get_input_type() for consistency.

-		$value = ! is_array( $value ) ? array( $value ) : $value;
-		$value = $field->type == 'email' ? array( $value[0] ) : $value;
+		$value = ! is_array( $value ) ? array( $value ) : $value;
+		// Email fields may have confirmation enabled (multi-input). When validating by field ID,
+		// ensure we only use the primary email input for uniqueness comparison.
+		if ( $field->get_input_type() === 'email' && ! $input_id ) {
+			if ( is_array( $value ) ) {
+				// Prefer the first sub-input's ID (primary email). Fallback to the first element.
+				$primary_input_id = is_array( rgar( $field, 'inputs' ) ) && ! empty( $field->inputs )
+					? rgar( $field->inputs[0], 'id' )
+					: null;
+				$primary_value = $primary_input_id && isset( $value[ $primary_input_id ] )
+					? $value[ $primary_input_id ]
+					: reset( $value );
+				$value = array( $primary_value );
+			} else {
+				$value = array( $value );
+			}
+		}

Quick QA checklist:

  • Email field (no confirmation) still compares correctly.
  • Email field with confirmation: two Email fields A and B with matching primary values should fail uniqueness even if confirmation values differ.
  • If configuration targets an individual email input ID (e.g., 5.2), this logic won’t override it because it’s gated by ! $input_id.
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
$value = ! is_array( $value ) ? array( $value ) : $value;
$value = $field->type == 'email' ? array( $value[0] ) : $value;
$value = array_filter( $value );
// Normalize to array
$value = ! is_array( $value ) ? array( $value ) : $value;
// Email fields may have confirmation enabled (multi-input). When validating by field ID,
// ensure we only use the primary email input for uniqueness comparison.
if ( $field->get_input_type() === 'email' && ! $input_id ) {
if ( is_array( $value ) ) {
// Prefer the first sub-input's ID (primary email). Fallback to the first element.
$primary_input_id = is_array( rgar( $field, 'inputs' ) ) && ! empty( $field->inputs )
? rgar( $field->inputs[0], 'id' )
: null;
$primary_value = $primary_input_id && isset( $value[ $primary_input_id ] )
? $value[ $primary_input_id ]
: reset( $value );
$value = array( $primary_value );
} else {
$value = array( $value );
}
}
// Remove any empty values
$value = array_filter( $value );
🤖 Prompt for AI Agents
In gravity-forms/gw-require-unique-values.php around lines 220 to 223, the code
assumes $value is numerically indexed and uses $value[0], which breaks
Email+Confirmation fields where $value is an associative array keyed by input
IDs (e.g., "3.1","3.2"); update the logic so when validating by field ID (i.e.,
when !$input_id) and the field get_input_type() is 'email', pick the primary
email sub-input ID from $field->inputs (the first non-empty input entry or the
one marked as primary) and use that key to extract the value from $value,
falling back to the first element value if that fails, then array_filter as
before — do not change behavior when an explicit $input_id is provided.

return $value;
Expand Down