-
Notifications
You must be signed in to change notification settings - Fork 92
gprf-clear-specific-field-values-on-reload.php: Fixed an issue where checkbox and file upload field values were not cleared as expected when reloading the form.
#1154
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
Conversation
…e checkbox and file upload field values were not cleared as expected when reloading the form.
WalkthroughIntroduces 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
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
Estimated code review effort🎯 2 (Simple) | ⏱️ ~10 minutes Suggested reviewers
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: 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.
📒 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.
| if ( $field->type === 'checkbox' ) { | ||
| foreach ( $_POST as $key => $value ) { | ||
| if ( strpos( $key, "input_{$field_id}" ) === 0 ) { | ||
| $_POST[ $key ] = ''; | ||
| } | ||
| } | ||
| } elseif ( $field->type === 'fileupload' ) { |
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: 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.
| 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.
| 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 { |
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.
🛠️ 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.
| 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.
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.