Fix foreach warning in contact form field validate() when value is a string#49503
Fix foreach warning in contact form field validate() when value is a string#49503agent-sandbox-automattic[bot] wants to merge 1 commit into
Conversation
…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>
|
Are you an Automattician? Please test your changes on all WordPress.com environments to help mitigate accidental explosions.
Interested in more tips and information?
|
|
Thank you for your PR! When contributing to Jetpack, we have a few suggestions that can help us test and review your patch:
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: 🔴 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: Follow this PR Review Process:
If you have questions about anything, reach out in #jetpack-developers for guidance! |
Code Coverage SummaryCannot generate coverage summary while tests are failing. 🤐 Please fix the tests, or re-run the Code coverage job if it was something being flaky. |
Problem
A PHP warning was being emitted from
class-contact-form-field.php:This occurred in the
validate()method in two places:checkbox-multiplecase (line 373) — iterates$field_valuedirectly.image-selectcase withismultipleenabled (line 486) — same pattern.$field_valueis built from$_POST[$field_id]. When the POST value is a scalar string (rather than an array), it remains a string after sanitization. Theempty()guard only skips the block for falsy values, so a non-empty string reaches theforeach, triggering the warning.Fix
Cast
$field_valueto(array)before bothforeachloops — 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.phpthat pass a string$_POSTvalue forcheckbox-multipleandimage-select(ismultiple) fields and assert thatvalidate()completes without a fatal error or warning, and correctly reports a validation error.Changelog
projects/packages/forms/changelog/fix-forms-691-foreach-warning-field-validateFixes FORMS-691.
Linear: FORMS-691