Skip to content

Conversation

saifsultanc
Copy link
Contributor

Copy link

coderabbitai bot commented Aug 26, 2025

Walkthrough

Updated 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

Cohort / File(s) Summary of Changes
Validation logic: email vs multi-input handling
gravity-forms/gw-require-unique-values.php
Adjusted get_filtered_value: skips concatenating sub-inputs when field type is email; after normalization, forces email values to a single-element array. Non-email multi-input fields unchanged.

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
Loading

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 Docstrings
🧪 Generate unit tests
  • Create PR with unit tests
  • Post copyable unit tests in a comment
  • Commit unit tests in branch saif/add/88275-add-support-email-field-confirmation

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.

❤️ Share
🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.

Support

Need help? Create a ticket on our support page for assistance with any issues or questions.

CodeRabbit Commands (Invoked using PR/Issue comments)

Type @coderabbitai help to get the list of available commands.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Status, Documentation and Community

  • Visit our Status Page to check the current availability of CodeRabbit.
  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

Copy link

Warnings
⚠️ When ready, don't forget to request reviews on this pull request from your fellow wizards.

Generated by 🚫 dangerJS against 0c0586e

Copy link

@coderabbitai coderabbitai bot left a 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.

📥 Commits

Reviewing files that changed from the base of the PR and between f6c5ada and 0c0586e.

📒 Files selected for processing (1)
  • gravity-forms/gw-require-unique-values.php (2 hunks)

Comment on lines 220 to 223
$value = ! is_array( $value ) ? array( $value ) : $value;
$value = $field->type == 'email' ? array( $value[0] ) : $value;
$value = array_filter( $value );

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.

@saifsultanc saifsultanc merged commit 91b6f40 into master Aug 29, 2025
4 checks passed
@saifsultanc saifsultanc deleted the saif/add/88275-add-support-email-field-confirmation branch August 29, 2025 06:17
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

1 participant