Skip to content

fix: text field validation rejecting localized object values#15932

Merged
PatrikKozak merged 1 commit into
payloadcms:mainfrom
omar-y-abdi:fix/15828-localized-required-text
Mar 12, 2026
Merged

fix: text field validation rejecting localized object values#15932
PatrikKozak merged 1 commit into
payloadcms:mainfrom
omar-y-abdi:fix/15828-localized-required-text

Conversation

@omar-y-abdi

@omar-y-abdi omar-y-abdi commented Mar 12, 2026

Copy link
Copy Markdown
Contributor

Summary

Fixes #15828

When using locale: 'all' with a required localized text field, the text validator incorrectly rejects the value. This happens because:

  1. With locale: 'all', the value passed to the validator is an object like {en: "Hello", es: "World"} (locale unflattening via mergeLocaleActions happens after validation in promise.ts)
  2. The old condition !(typeof value === 'string' || Array.isArray(value)) evaluates to true for objects, triggering the required error
  3. The textarea validator already handles this correctly by using !value (objects are truthy)

Fix

Changed the text validator's required check from:

if (!(typeof value === 'string' || Array.isArray(value)) || value?.length === 0)

to:

if (!value || ((typeof value === 'string' || Array.isArray(value)) && value.length === 0))

This aligns with the textarea validator's approach:

  • !value catches null, undefined, and empty string ""
  • The second clause catches empty arrays [] (truthy but length 0)
  • Localized objects like {en: "Hello"} pass because !obj is false

Changes

  • packages/payload/src/fields/validations.ts — 1-line fix to the required check condition
  • packages/payload/src/fields/validations.spec.ts — 3 unit tests (localized object accepted, null rejected, empty string rejected)
  • test/fields/collections/Text/index.ts — Added localizedRequiredText test field
  • test/fields/int.spec.ts — Integration test verifying locale: 'all' with required localized text

Test plan

  • Unit tests: 88/88 passed (85 existing + 3 new)
  • Integration tests (SQLite): 154/154 passed
  • CI passes on all database adapters

…t values

When using `locale: 'all'` with a required localized text field, the
validator receives an object like `{en: "Hello", es: "World"}` instead
of a string. The previous condition `!(typeof value === 'string' ||
Array.isArray(value))` incorrectly rejected these objects.

Aligned the text validator's required check with the textarea validator:
first check `!value` (catches null/undefined/empty string), then check
if string/array has length 0 (catches empty arrays).

Fixes payloadcms#15828
@omar-y-abdi omar-y-abdi changed the title fix(payload): resolve text field validation rejecting localized object values fix: resolve text field validation rejecting localized object values Mar 12, 2026
@PatrikKozak PatrikKozak self-assigned this Mar 12, 2026
@PatrikKozak PatrikKozak self-requested a review March 12, 2026 20:32
@PatrikKozak PatrikKozak changed the title fix: resolve text field validation rejecting localized object values fix: text field validation rejecting localized object values Mar 12, 2026
@PatrikKozak

Copy link
Copy Markdown
Contributor

@omar-y-abdi Fix looks good!

Thank you for the contribution - going to merge

@PatrikKozak PatrikKozak merged commit fac59c8 into payloadcms:main Mar 12, 2026
158 of 159 checks passed
@github-actions

Copy link
Copy Markdown
Contributor

🚀 This is included in version v3.79.1

@marcusforsberg

Copy link
Copy Markdown
Contributor

@PatrikKozak Any chance something similar could be causing #15651? I just tested 3.79.1 in the hopes that this fix would happen to address that issue as well, but unfortunately that doesn't seem to be the case.

Sorry for tagging you, just hoping to get some attention to that issue in case it has slipped between the cracks. 🙏

@PatrikKozak

Copy link
Copy Markdown
Contributor

Hey @marcusforsberg - I'll put this on my radar and get back to you once I've had a chance to take a look

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.

Payload bug / inconsistency in how text handles localized + required

3 participants