Skip to content

Conversation

@saifsultanc
Copy link
Contributor

Context

⛑️ Ticket(s): https://secure.helpscout.net/conversation/3053111690/88474

Summary

Fixed an issue where checkbox and file upload field values were not cleared as expected when reloading the form.

…e checkbox and file upload field values were not cleared as expected when reloading the form.
@coderabbitai
Copy link

coderabbitai bot commented Aug 29, 2025

Walkthrough

Introduces a per-form Gravity Forms pre-render hook for form 123 that clears specified fields (2, 3, 4) on reload, with type-aware handling for checkboxes and file uploads. Replaces a previous global pre-render hook and named function with an anonymous closure that returns the modified form.

Changes

Cohort / File(s) Summary
Gravity Forms pre-render hook update
gp-reload-form/gprf-clear-specific-field-values-on-reload.php
Replaces global gform_pre_render named handler with gform_pre_render_123 anonymous closure. Clears multiple fields (2,3,4) on reload: resolves fields via GFAPI::get_field; for checkboxes clears all related $_POST inputs; for fileuploads updates gform_uploaded_files and GFFormsModel::$uploaded_files; otherwise clears $_POST["input_<id>"]. Removes prior self-removal logic.

Sequence Diagram(s)

sequenceDiagram
  autonumber
  participant U as User
  participant WP as WordPress
  participant GF as Gravity Forms
  participant H as Pre-render Hook (gform_pre_render_123)
  participant API as GFAPI/GFFormsModel
  participant ENV as $_POST / gform_uploaded_files

  U->>WP: Request form 123 (reload)
  WP->>GF: Render form 123
  GF->>H: Trigger pre-render callback
  H->>API: Resolve fields (2,3,4) via GFAPI::get_field
  alt Checkbox field
    H->>ENV: Unset $_POST["input_<id>.*"]
  else File upload field
    H->>ENV: Remove uploaded files for input_<id>
    H->>API: Reset GFFormsModel::$uploaded_files[field_id]
  else Other field
    H->>ENV: Unset $_POST["input_<id>"]
  end
  H-->>GF: Return modified form
  GF-->>WP: Continue render
  WP-->>U: Form 123 with cleared values
Loading

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~10 minutes

Suggested reviewers

  • veryspry

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/fix/88474-fix-gprf-specific-checkbox-fileupload

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 @coderabbit in a new review comment at the desired location with your query.
  • PR comments: Tag @coderabbit 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:
    • @coderabbit gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbit 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 @coderabbit help to get the list of available commands.

Other keywords and placeholders

  • Add @coderabbit ignore or @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbit summary or @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbit or @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.

@github-actions
Copy link

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

Generated by 🚫 dangerJS against 2f52894

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: 2

Caution

Some comments are outside the diff and can’t be posted inline due to platform limitations.

⚠️ Outside diff range comments (1)
gp-reload-form/gprf-clear-specific-field-values-on-reload.php (1)

3-3: Typo in header: “Reload From” → “Reload Form”.

User-facing docs should be accurate for discoverability.

Apply this diff:

- * Gravity Perks // Reload From // Clear Specific Field Values on Reload
+ * Gravity Perks // Reload Form // Clear Specific Field Values on Reload
🧹 Nitpick comments (2)
gp-reload-form/gprf-clear-specific-field-values-on-reload.php (2)

13-16: Register the pre-render filter with an explicit priority.

Ensure this runs after Reload Form repopulates preserved values by setting a later priority (e.g., 20).

Apply this diff:

-	add_filter( 'gform_pre_render_' . $form_id, function( $form ) use ( $fields_to_clear ) {
+	add_filter( 'gform_pre_render_' . $form_id, function( $form ) use ( $fields_to_clear ) {
 		// …
-	}, 10 );
+	}, 20 );

Please verify in a reload flow that preserved values populate first, then the targeted fields are cleared.


11-16: Consider registering directly on gform_pre_render_{$form_id} (optional).

Coupling to gprf_disable_dynamic_reload_{$form_id} may be brittle if that filter runs earlier/later than expected across versions. If you keep this approach, ensure it fires exactly once per request.

If you want, I can provide a variant that attaches both to gform_pre_render_{$form_id} and gform_pre_validation_{$form_id} at priority 20 to cover display and validation paths.

📜 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 91b6f40 and 2f52894.

📒 Files selected for processing (1)
  • gp-reload-form/gprf-clear-specific-field-values-on-reload.php (1 hunks)
🔇 Additional comments (3)
gp-reload-form/gprf-clear-specific-field-values-on-reload.php (3)

18-24: Good: defensive field resolution.

Gracefully skipping missing fields prevents notices when IDs drift.


41-41: OK for single-input fields.

Directly blanking the canonical input key is correct for non-complex, non-file fields.


45-46: Return value is correct.

gform_pre_render_* requires returning the (possibly) modified $form.

Comment on lines +25 to +31
if ( $field->type === 'checkbox' ) {
foreach ( $_POST as $key => $value ) {
if ( strpos( $key, "input_{$field_id}" ) === 0 ) {
$_POST[ $key ] = '';
}
}
} elseif ( $field->type === 'fileupload' ) {
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: prefix match can clear the wrong fields (e.g., field 2 matches input_20.*).

Use an exact-key or “dot-delimited” boundary check for checkbox inputs.

Apply this diff:

-			if ( $field->type === 'checkbox' ) {
+			if ( $field->type === 'checkbox' ) {
 				foreach ( $_POST as $key => $value ) {
-					if ( strpos( $key, "input_{$field_id}" ) === 0 ) {
-						$_POST[ $key ] = '';
-					}
+					// Match either "input_<id>" or "input_<id>.<choice>"
+					if ( $key === "input_{$field_id}" || strpos( $key, "input_{$field_id}." ) === 0 ) {
+						unset( $_POST[ $key ] );
+					}
 				}
📝 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
if ( $field->type === 'checkbox' ) {
foreach ( $_POST as $key => $value ) {
if ( strpos( $key, "input_{$field_id}" ) === 0 ) {
$_POST[ $key ] = '';
}
}
} elseif ( $field->type === 'fileupload' ) {
if ( $field->type === 'checkbox' ) {
foreach ( $_POST as $key => $value ) {
// Match either "input_<id>" or "input_<id>.<choice>"
if ( $key === "input_{$field_id}" || strpos( $key, "input_{$field_id}." ) === 0 ) {
unset( $_POST[ $key ] );
}
}
} elseif ( $field->type === 'fileupload' ) {
🤖 Prompt for AI Agents
In gp-reload-form/gprf-clear-specific-field-values-on-reload.php around lines 25
to 31, the current prefix check strpos($key, "input_{$field_id}") === 0 can
match unintended fields like input_20 when field_id is 2; change the condition
to only clear exact keys or keys that use the field boundary (e.g., key ===
"input_{$field_id}" OR strpos($key, "input_{$field_id}_") === 0 OR strpos($key,
"input_{$field_id}.") === 0) so only the intended checkbox inputs are cleared.

Comment on lines +32 to +40
if ( isset( $_POST['gform_uploaded_files'] ) ) {
$uploaded_files = json_decode( stripslashes( $_POST['gform_uploaded_files'] ), true );
if ( isset( $uploaded_files[ "input_{$field_id}" ] ) ) {
unset( $uploaded_files[ "input_{$field_id}" ] );
$_POST['gform_uploaded_files'] = wp_json_encode( $uploaded_files );
}
}
GFFormsModel::$uploaded_files[ $form['id'] ][ "input_{$field_id}" ] = array();
} else {
Copy link

Choose a reason for hiding this comment

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

🛠️ Refactor suggestion

Harden file upload clearing: unslashing, type checks, and static array init.

Avoids edge-case warnings and ensures the static cache is always an array.

Apply this diff:

-			} elseif ( $field->type === 'fileupload' ) {
-				if ( isset( $_POST['gform_uploaded_files'] ) ) {
-					$uploaded_files = json_decode( stripslashes( $_POST['gform_uploaded_files'] ), true );
-					if ( isset( $uploaded_files[ "input_{$field_id}" ] ) ) {
-						unset( $uploaded_files[ "input_{$field_id}" ] );
-						$_POST['gform_uploaded_files'] = wp_json_encode( $uploaded_files );
-					}
-				}
-				GFFormsModel::$uploaded_files[ $form['id'] ][ "input_{$field_id}" ] = array();
+			} elseif ( $field->type === 'fileupload' ) {
+				if ( isset( $_POST['gform_uploaded_files'] ) ) {
+					$uploaded_files = json_decode( wp_unslash( $_POST['gform_uploaded_files'] ), true );
+					if ( is_array( $uploaded_files ) && isset( $uploaded_files[ "input_{$field_id}" ] ) ) {
+						unset( $uploaded_files[ "input_{$field_id}" ] );
+						$_POST['gform_uploaded_files'] = wp_json_encode( $uploaded_files );
+					}
+				}
+				if ( ! is_array( GFFormsModel::$uploaded_files ) ) {
+					GFFormsModel::$uploaded_files = array();
+				}
+				if ( ! isset( GFFormsModel::$uploaded_files[ $form['id'] ] ) || ! is_array( GFFormsModel::$uploaded_files[ $form['id'] ] ) ) {
+					GFFormsModel::$uploaded_files[ $form['id'] ] = array();
+				}
+				GFFormsModel::$uploaded_files[ $form['id'] ][ "input_{$field_id}" ] = array();
📝 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
if ( isset( $_POST['gform_uploaded_files'] ) ) {
$uploaded_files = json_decode( stripslashes( $_POST['gform_uploaded_files'] ), true );
if ( isset( $uploaded_files[ "input_{$field_id}" ] ) ) {
unset( $uploaded_files[ "input_{$field_id}" ] );
$_POST['gform_uploaded_files'] = wp_json_encode( $uploaded_files );
}
}
GFFormsModel::$uploaded_files[ $form['id'] ][ "input_{$field_id}" ] = array();
} else {
} elseif ( $field->type === 'fileupload' ) {
if ( isset( $_POST['gform_uploaded_files'] ) ) {
$uploaded_files = json_decode( wp_unslash( $_POST['gform_uploaded_files'] ), true );
if ( is_array( $uploaded_files ) && isset( $uploaded_files[ "input_{$field_id}" ] ) ) {
unset( $uploaded_files[ "input_{$field_id}" ] );
$_POST['gform_uploaded_files'] = wp_json_encode( $uploaded_files );
}
}
// Ensure the static cache is initialized
if ( ! is_array( GFFormsModel::$uploaded_files ) ) {
GFFormsModel::$uploaded_files = array();
}
if ( ! isset( GFFormsModel::$uploaded_files[ $form['id'] ] )
|| ! is_array( GFFormsModel::$uploaded_files[ $form['id'] ] ) ) {
GFFormsModel::$uploaded_files[ $form['id'] ] = array();
}
GFFormsModel::$uploaded_files[ $form['id'] ][ "input_{$field_id}" ] = array();
} else {
🤖 Prompt for AI Agents
In gp-reload-form/gprf-clear-specific-field-values-on-reload.php around lines 32
to 40, the code that clears uploaded files should first wp_unslash the raw
$_POST['gform_uploaded_files'] and confirm it's a string before calling
json_decode; after json_decode verify the result is an array (or fallback to an
empty array) before accessing or unsetting input keys; when writing back use
wp_json_encode of the ensured-array; and always ensure
GFFormsModel::$uploaded_files is initialized as an array and that
GFFormsModel::$uploaded_files[ $form['id'] ][ "input_{$field_id}" ] is set to an
array (not null) to avoid warnings and preserve the static cache shape.

@saifsultanc saifsultanc merged commit 0027381 into master Sep 1, 2025
4 checks passed
@saifsultanc saifsultanc deleted the saif/fix/88474-fix-gprf-specific-checkbox-fileupload branch September 1, 2025 06:46
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.

2 participants