Skip to content

Guard text field maxlength check against non-scalar values#464

Merged
cbravobernal merged 2 commits into
trunkfrom
fix/text-field-nonscalar-value
Jun 12, 2026
Merged

Guard text field maxlength check against non-scalar values#464
cbravobernal merged 2 commits into
trunkfrom
fix/text-field-nonscalar-value

Conversation

@cbravobernal

Copy link
Copy Markdown
Contributor

Description

The text and textarea field types validate a submitted value against their configured maxlength by calling acf_strlen( $value ). That helper runs string operations (wp_unslash, str_replace, mb_strlen) directly on the value.

A form submission can deliver a non-scalar value (for example an array) for one of these fields. When that happens with a maxlength set, the maxlength check ran a string operation on an array and emitted an Array to string conversion notice. A non-scalar value is not valid text and should simply be treated as invalid rather than producing a diagnostic.

Change

Guard the maxlength check with is_scalar( $value ) in both field types so a non-scalar value skips the string-length operation:

  • includes/fields/class-acf-field-text.phpvalidate_value()
  • includes/fields/class-acf-field-textarea.phpvalidate_value()

Behavior for normal string values is unchanged. A non-scalar value preserves the passed validity state, matching the field's existing handling of values that are not valid text. The same code path exists in the upstream-derived source, so the same guard applies there.

Tests

Added regression tests asserting that validating a non-scalar value with a maxlength set does not emit an array-to-string conversion and preserves the passed validity state:

  • Test_ACF_Field_Text::test_validate_value_array_with_maxlength_does_not_convert
  • Test_ACF_Field_Textarea::test_validate_value_array_with_maxlength_does_not_convert

The tests fail against the unpatched code (the conversion notice fires) and pass with the guard.

Verification

  • composer test:php -- --filter 'Field_Text' — green (23 tests)
  • composer test:php — green (2266 tests, 4306 assertions)
  • composer test:phpstan — no errors
  • vendor/bin/phpcs — clean on the changed test files; the changed source lines add no new findings (pre-existing upstream-derived style findings are unchanged)

Use of AI Tools

This change was authored with Claude Code under human direction. The author reviewed and takes responsibility for the change.

cbravobernal and others added 2 commits June 12, 2026 12:19
The text and textarea field types run acf_strlen() on the submitted
value when a maxlength is configured. A submission can deliver a
non-scalar value (e.g. an array) for these fields, which made
acf_strlen() emit an "Array to string conversion" notice.

Guard the maxlength check with is_scalar() so a non-scalar value is
treated as invalid text and skips the string length operation, leaving
behavior for normal strings unchanged. The same code path exists in the
upstream-derived source.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
Cover that validating a non-scalar value against a text or textarea
field with a maxlength set does not emit an "Array to string conversion"
notice and preserves the passed validity state.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
@github-actions

Copy link
Copy Markdown

The following accounts have interacted with this PR and/or linked issues. I will continue to update these lists as activity occurs. You can also manually ask me to refresh this list by adding the props-bot label.

Core Committers: Use this line as a base for the props when committing in SVN:

Props cbravobernal.

To understand the WordPress project's expectations around crediting contributors, please review the Contributor Attribution page in the Core Handbook.

@cbravobernal cbravobernal merged commit 3098705 into trunk Jun 12, 2026
19 checks passed
@cbravobernal cbravobernal deleted the fix/text-field-nonscalar-value branch June 12, 2026 10:40
@cbravobernal cbravobernal added the [Type] Bug Something isn't working label Jun 12, 2026
@cbravobernal cbravobernal modified the milestones: 6.9.0, 6.8.9 Jun 12, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

[Type] Bug Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant