Skip to content

Conversation

@faisalahammad
Copy link

@faisalahammad faisalahammad commented Jan 30, 2026

Summary

Fixes #7475 - Adds an option to preserve conditional field values when the field is hidden by conditional logic.

Problem

When a field is hidden by conditional logic, saving the form clears the field's value in the database. This happens because:

  1. PodsMeta.php initializes ALL fields with an empty string default
  2. Hidden fields aren't submitted via $_POST
  3. The empty value overwrites the existing database value

Solution

Added a new field option "Save Value When Hidden" (conditional_logic_save_value) that:

  • Appears in the Conditional Logic tab for fields with conditional logic enabled
  • When enabled, skips adding the field to the save data if it wasn't submitted (hidden)
  • This preserves the existing database value instead of clearing it

Changes

src/Pods/Admin/Config/Field.php

Added new conditional_logic_save_value boolean option to field configuration.

classes/PodsMeta.php

Before:

if ( ! pods_permission( $field ) ) {
    if ( 1 !== (int) pods_v( 'hidden', $field, 0 ) ) {
        continue;
    }
}

$data[ $field['name'] ] = '';

if ( isset( $_POST[ 'pods_meta_' . $field['name'] ] ) ) {
    $data[ $field['name'] ] = $_POST[ 'pods_meta_' . $field['name'] ];
}

After:

if ( ! pods_permission( $field ) ) {
    if ( 1 !== (int) pods_v( 'hidden', $field, 0 ) ) {
        continue;
    }
}

// Check if field wasn't submitted (likely hidden by conditional logic).
if ( ! isset( $_POST[ 'pods_meta_' . $field['name'] ] ) ) {
    // If save_value_when_hidden is enabled, skip this field to preserve existing DB value.
    $conditional_logic_save = (bool) pods_v( 'conditional_logic_save_value', $field, false );

    if ( $conditional_logic_save ) {
        continue;
    }
}

$data[ $field['name'] ] = '';

if ( isset( $_POST[ 'pods_meta_' . $field['name'] ] ) ) {
    $data[ $field['name'] ] = $_POST[ 'pods_meta_' . $field['name'] ];
}

classes/PodsAPI.php

Before:

// Skip if the field is visible and can be saved.
if ( $is_visible ) {
    continue;
}

// Check if the field should save its value even when hidden.
$save_value_when_hidden = (bool) pods_v( 'conditional_logic_save_value', $fields[ $active_field_name ], false );

// Skip clearing the value if save_value_when_hidden is enabled.
if ( $save_value_when_hidden ) {
    continue;
}

// Unset the field value.
$field_values[ $active_field_name ]    = null;
$fields[ $active_field_name ]['value'] = null;

After:

// Skip if the field is visible and can be saved.
if ( $is_visible ) {
    continue;
}

// Unset the field value.
$field_values[ $active_field_name ]    = null;
$fields[ $active_field_name ]['value'] = null;

Testing

  1. Create a Pod with conditional logic (e.g., Show Banner = Yes/No)
  2. Add a conditional field (e.g., Banner Content) that shows when Show Banner = Yes
  3. Edit the field and enable "Preserve field value when conditionally hidden"
  4. Enter content in Banner Content and save
  5. Set Show Banner = No and save
  6. Reload the page and set Show Banner = Yes
  7. ✅ Banner Content value is preserved

Checklist

  • Code follows WordPress coding standards
  • Changes tested manually
  • No breaking changes
  • Feature is opt-in (requires enabling the option)

Screen Recording

Before: https://videos.faisalahammad.com/recordings/hR9KDdIgMfSUNC1nh3hm

After: https://videos.faisalahammad.com/recordings/P91zwf3QD774GdGqLovP

Plugin Zip

pods-issue-7475-save-conditional-value.zip

This commit adds a new 'Save Value When Hidden' option for fields with conditional logic.

Problem: When a conditional field's parent condition is set to false, child field values get reset to null (Issue pods-framework#7475).

Solution:
- Added 'conditional_logic_save_value' boolean option in Field.php
- Modified PodsAPI.php to check this option before clearing hidden field values
- When enabled, the field value is preserved even when the field is not visible

Fixes pods-framework#7475
@what-the-diff
Copy link

what-the-diff bot commented Jan 30, 2026

PR Summary

  • Improved Field Validation Mechanism
    This update introduces a refined logic that detects whether a field was omitted because of certain conditions, thus enabling users to avoid the saving process when the field is hidden.

  • New Configurable Option for Conditionally Hidden Fields
    We have incorporated a new setting: 'conditional_logic_save_value'. This allows users to save values of fields even in circumstances where these fields are hidden by conditional logic.

@sclark3-godaddy
Copy link

@faisalahammad hey, just checking in on this -- did you intend to close the PR?

…ork#7475)

Added 'conditional_logic_save_value' option to field configuration that
allows preserving field values when the field is hidden by conditional logic.

Changes:
- Field.php: Added new 'conditional_logic_save_value' boolean option
- PodsMeta.php: Skip adding hidden fields to save data when option is enabled

This prevents the empty default value from overwriting existing database
values when conditional logic hides a field during form submission.
@faisalahammad faisalahammad changed the title Add option to save conditional field values when hidden Fix: Preserve conditional field values when hidden (issue #7475) Jan 30, 2026
@faisalahammad faisalahammad reopened this Jan 30, 2026
@faisalahammad
Copy link
Author

Sorry, @sclark3-godaddy
I mistakenly pushed the PR without fully working code, so I fixed it and reopened the PR. Could you please check and let me know if it’s working on your end?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Option to Save Value of Conditional Field.

2 participants