-
Notifications
You must be signed in to change notification settings - Fork 93
gw-require-unique-values.php
: Added support for email field with confirmation.
#1152
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
gw-require-unique-values.php
: Added support for email field with confirmation.
#1152
Conversation
WalkthroughUpdated get_filtered_value in gravity-forms/gw-require-unique-values.php to treat email fields as single-value inputs: exclude them from multi-input concatenation and coerce their values to a single-element array post-normalization. Non-email multi-input fields retain existing concatenation behavior. Changes
Sequence Diagram(s)sequenceDiagram
autonumber
participant U as User
participant GF as Gravity Forms
participant GW as gw-require-unique-values (get_filtered_value)
participant DB as Uniqueness Store
U->>GF: Submit form
GF->>GW: get_filtered_value(field, value)
alt Field is email
note over GW: Do not concatenate sub-inputs<br/>Coerce to [value[0]]
else Field is multi-input (non-email)
note over GW: Concatenate subfield values<br/>when using field ID
end
GW->>DB: Check uniqueness with normalized value
DB-->>GW: Result
GW-->>GF: Normalized value / validation outcome
GF-->>U: Validation success/failure
Estimated code review effort🎯 2 (Simple) | ⏱️ ~10 minutes Possibly related PRs
Tip 🔌 Remote MCP (Model Context Protocol) integration is now available!Pro plan users can now connect to remote MCP servers from the Integrations page. Connect with popular remote MCPs such as Notion and Linear to add more context to your reviews and chats. ✨ Finishing Touches
🧪 Generate unit tests
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. 🪧 TipsChatThere are 3 ways to chat with CodeRabbit:
SupportNeed help? Create a ticket on our support page for assistance with any issues or questions. CodeRabbit Commands (Invoked using PR/Issue comments)Type Other keywords and placeholders
CodeRabbit Configuration File (
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actionable comments posted: 1
🧹 Nitpick comments (1)
gravity-forms/gw-require-unique-values.php (1)
205-218
: Correctly excluding Email fields from sub-input concatenation; consider using get_input_type()Good call to skip concatenation for Email fields so confirmation inputs don’t pollute the uniqueness token. For consistency with the rest of the file (e.g., Lines 65–67) and GF best practices, prefer
$field->get_input_type()
over$field->type
.Apply:
- if ( ! $input_id && is_array( $field->inputs ) && is_array( $value ) && $field->type != 'email' ) { + if ( ! $input_id && is_array( $field->inputs ) && is_array( $value ) && $field->get_input_type() !== 'email' ) {
📜 Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro
💡 Knowledge Base configuration:
- MCP integration is disabled by default for public repositories
- Jira integration is disabled
- Linear integration is disabled
You can enable these sources in your CodeRabbit configuration.
📒 Files selected for processing (1)
gravity-forms/gw-require-unique-values.php
(2 hunks)
$value = ! is_array( $value ) ? array( $value ) : $value; | ||
$value = $field->type == 'email' ? array( $value[0] ) : $value; | ||
$value = array_filter( $value ); | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
$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.
Context
⛑️ Ticket(s): https://secure.helpscout.net/conversation/3048257456/88275
Summary
Added support for email field with confirmation.
BEFORE:
https://www.loom.com/share/06c1ff1ee9c245d0b7bf87daad9e863b
AFTER:
https://www.loom.com/share/22fc6e3fe3824a6492325ca885b80c86