Skip to content

Fix foreach warning in contact form field validate() when value is a string#49503

Draft
agent-sandbox-automattic[bot] wants to merge 1 commit into
trunkfrom
ai/agent/forms-691-1781060618
Draft

Fix foreach warning in contact form field validate() when value is a string#49503
agent-sandbox-automattic[bot] wants to merge 1 commit into
trunkfrom
ai/agent/forms-691-1781060618

Conversation

@agent-sandbox-automattic

Copy link
Copy Markdown
Contributor

Problem

A PHP warning was being emitted from class-contact-form-field.php:

foreach() argument must be of type array|object, string given

This occurred in the validate() method in two places:

  1. The checkbox-multiple case (line 373) — iterates $field_value directly.
  2. The image-select case with ismultiple enabled (line 486) — same pattern.

$field_value is built from $_POST[$field_id]. When the POST value is a scalar string (rather than an array), it remains a string after sanitization. The empty() guard only skips the block for falsy values, so a non-empty string reaches the foreach, triggering the warning.

Fix

Cast $field_value to (array) before both foreach loops — consistent with the pattern already used elsewhere in the same file (e.g., foreach ( (array) $this->get_attribute( 'options' ) as … )).

Testing

Added two new unit tests in Contact_Form_Field_Test.php that pass a string $_POST value for checkbox-multiple and image-select (ismultiple) fields and assert that validate() completes without a fatal error or warning, and correctly reports a validation error.

Changelog

projects/packages/forms/changelog/fix-forms-691-foreach-warning-field-validate

Fixes FORMS-691.


Linear: FORMS-691

This PR was auto-generated by Agent Sandbox. Please review carefully before merging.

…string

In the `checkbox-multiple` and `image-select` (ismultiple) validation
branches, `$field_value` can be a plain string when `$_POST` contains a
scalar value rather than an array. Cast to `(array)` before iterating to
prevent the "foreach() argument must be of type array|object, string given"
PHP warning.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
@github-actions

github-actions Bot commented Jun 10, 2026

Copy link
Copy Markdown
Contributor

Are you an Automattician? Please test your changes on all WordPress.com environments to help mitigate accidental explosions.

  • To test on WoA, go to the Plugins menu on a WoA dev site. Click on the "Upload" button and follow the upgrade flow to be able to upload, install, and activate the Jetpack Beta plugin. Once the plugin is active, go to Jetpack > Jetpack Beta, select your plugin (Jetpack), and enable the ai/agent/forms-691-1781060618 branch.
  • To test on Simple, run the following command on your sandbox:
bin/jetpack-downloader test jetpack ai/agent/forms-691-1781060618

Interested in more tips and information?

  • In your local development environment, use the jetpack rsync command to sync your changes to a WoA dev blog.
  • Read more about our development workflow here: PCYsg-eg0-p2
  • Figure out when your changes will be shipped to customers here: PCYsg-eg5-p2

@github-actions

Copy link
Copy Markdown
Contributor

Thank you for your PR!

When contributing to Jetpack, we have a few suggestions that can help us test and review your patch:

  • ✅ Include a description of your PR changes.
  • 🔴 Add a "[Status]" label (In Progress, Needs Review, ...).
  • 🔴 Add testing instructions.
  • 🔴 Specify whether this PR includes any changes to data or privacy.
  • ✅ Add changelog entries to affected projects

This comment will be updated as you work on your PR and make changes. If you think that some of those checks are not needed for your PR, please explain why you think so. Thanks for cooperation 🤖


🔴 Action required: Please include detailed testing steps, explaining how to test your change, like so:

## Testing instructions:

* Go to '..'
*

🔴 Action required: We would recommend that you add a section to the PR description to specify whether this PR includes any changes to data or privacy, like so:

## Does this pull request change what data or activity we track or use?

My PR adds *x* and *y*.

Follow this PR Review Process:

  1. Ensure all required checks appearing at the bottom of this PR are passing.
  2. Make sure to test your changes on all platforms that it applies to. You're responsible for the quality of the code you ship.
  3. You can use GitHub's Reviewers functionality to request a review.
  4. When it's reviewed and merged, you will be pinged in Slack to deploy the changes to WordPress.com simple once the build is done.

If you have questions about anything, reach out in #jetpack-developers for guidance!

@github-actions github-actions Bot added the [Status] Needs Author Reply We need more details from you. This label will be auto-added until the PR meets all requirements. label Jun 10, 2026
@jp-launch-control

Copy link
Copy Markdown

Code Coverage Summary

Cannot generate coverage summary while tests are failing. 🤐

Please fix the tests, or re-run the Code coverage job if it was something being flaky.

Full summary · PHP report · JS report

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

Labels

[Feature] Contact Form [Package] Forms [Status] In Progress [Status] Needs Author Reply We need more details from you. This label will be auto-added until the PR meets all requirements. [Tests] Includes Tests

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant