Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Can not reset optional fields like text #11398

Open
cschroeter opened this issue Feb 26, 2025 · 0 comments
Open

Can not reset optional fields like text #11398

cschroeter opened this issue Feb 26, 2025 · 0 comments
Assignees

Comments

@cschroeter
Copy link

Describe the Bug

Issue: No Proper Way to Clear/Reset an Optional Field

There is no reliable way to clear or reset an optional field.

Example: User Collection

In this example, the email field is optional. Creating a user without an email results in:

{
    "id": 1,
    "name": "Max",
    "email": null
}

After adding an email:

{
    "id": 1,
    "name": "Max",
    "email": "max@test.com"
}

However, attempting to "remove" the email results in:

{
    "id": 1,
    "name": "Max",
    "email": ""
}

Expected behavior:

{
    "id": 1,
    "name": "Max",
    "email": null
}

Why This Is a Problem

  • While JavaScript can sometimes treat an empty string as falsy, it’s not a true null value.
  • This inconsistency complicates handling optional fields.
  • The issue is even worse for rich text fields. Instead of resetting to null, they transform into an unnecessary structured object:
"bio": {
    "root": {
        "type": "root",
        "format": "",
        "indent": 0,
        "version": 1,
        "children": [
            {
                "type": "paragraph",
                "format": "",
                "indent": 0,
                "version": 1,
                "children": [],
                "direction": null,
                "textStyle": "",
                "textFormat": 0
            }
        ],
        "direction": null
    }
}

Suggested Fix

Ensure that clearing an optional field properly resets it to null, maintaining consistency across all field types.

Link to the code that reproduces this issue

https://github.com/payloadcms/payload/tree/main/templates/website

Reproduction Steps

  1. Create a user without an email → email is null.
  2. Update the user by adding an email.
  3. Attempt to clear the email (e.g., set it to null or remove the field).
  4. Observe that email is stored as an empty string ("") instead of null.

Which area(s) are affected? (Select all that apply)

Not sure

Environment Info

Payload: Latest
Next.js: Latest
Node: 20+
@cschroeter cschroeter added status: needs-triage Possible bug which hasn't been reproduced yet validate-reproduction labels Feb 26, 2025
@GermanJablo GermanJablo self-assigned this Feb 26, 2025
@github-actions github-actions bot removed the status: needs-triage Possible bug which hasn't been reproduced yet label Feb 26, 2025
# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants