Skip to content
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

Can not save empty value in option page #1509

Open
cawa-93 opened this issue Feb 7, 2024 · 3 comments
Open

Can not save empty value in option page #1509

cawa-93 opened this issue Feb 7, 2024 · 3 comments

Comments

@cawa-93
Copy link

cawa-93 commented Feb 7, 2024

Describe the bug

When I create several fields on the settings page, I cannot save the state when all fields are empty.

Steps to reproduce

  1. Create metabox like this:
    add_action( 'cmb2_admin_init', function () {
        $cmb = new_cmb2_box( [
            'id' => 'box-id',
            'title' => 'BOX Title',
            'object_types' => [ 'options-page' ],
            'option_key' => 'box-key',
            'parent_slug' => 'options-general.php',
        ] );;
    
        $cmb->add_field([
            'id' => 'first',
            'name' => 'first',
            'type' => 'text'
        ]);
    
        $cmb->add_field([
            'id' => 'second',
            'name' => 'second',
            'type' => 'text',
        ]);
    } );
  2. Open option page where are metabox placed.
  3. Write foo as value in first input
  4. Save Changes
  5. Remove value from first input and leave it empty
  6. Save Changes

Expected behavior

Both fields are empty

Actual behavior

The first field keeps the value foo

Your Environment

CMB2 v2.10.1
php 8.1

@tw2113
Copy link
Contributor

tw2113 commented Feb 7, 2024

I know this type of topic has come up in the past, regardless of it being an option or post meta keys, but I can't recall if we had reasoning behind removing the key as well as the value. I guess one question I have is why you want to keep the option key even if empty. Is there logical reason you need that? What about using the default argument for the fields you're creating, for cases where a value doesn't exist yet but you still need something

@cawa-93
Copy link
Author

cawa-93 commented Feb 7, 2024

why you want to keep the option key even if empty

I don't need it. I don't do anything special for this. I just need to be able to delete a field value and that's it.

This works fine for the second field. You can set the value and clear it.
However, the first field cannot be cleared if the second field is also empty (if there are no other keys in the database).

Because of this, it is not possible to delete the value for the first field in the interfaces at all, only to manipulate the database directly.

I don't care how the data is stored, whether it's a key with an empty string, an empty array, or even deleting the field from the database.

To achieve my needs, I have not come up with any other workaround than to insert another field, which will do nothing, as the very first field.

$cmb = new_cmb2_box(/* ... */);

// This should be the first field to avoid issue 
// https://github.com/CMB2/CMB2/issues/1509
$cmb->add_field( [
    'id' => '__INTERNAL__',
    'type' => 'hidden',
    'sanitization_cb' => false,
    'escape_cb' => false,
    'default' => '1',
] );

// ... Rest field initialization

@tw2113
Copy link
Contributor

tw2113 commented Feb 7, 2024

Overall definitely sounding like a bug, from some internal discussion and what you've provided thus far.

We'll need to dig in and see what's going on though, as we don't have any immediate leads.

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

No branches or pull requests

2 participants