Skip to content

Fix - Prevent TypeError in CheckboxesField::regex() when stored value is invalid JSON#3647

Open
RomainLvr wants to merge 1 commit into
support/2.13.0from
fix/checkboxesfield-regex-null-value
Open

Fix - Prevent TypeError in CheckboxesField::regex() when stored value is invalid JSON#3647
RomainLvr wants to merge 1 commit into
support/2.13.0from
fix/checkboxesfield-regex-null-value

Conversation

@RomainLvr

Copy link
Copy Markdown

Changes description

Checklist

Please check if your PR fulfills the following specifications:

  • Tests for the changes have been added
  • Docs have been added/updated

Description

  • It fixes !44948
  • Here is a brief description of what this PR does

When rendering the plugin_formcreator_solved_issues dashboard widget, a TypeError is thrown:

preg_grep(): Argument #2 ($array) must be of type array, null given

This happens when a form answer contains a checkbox field with a regex visibility condition, and the stored JSON value is corrupted or invalid. json_decode() returns null on invalid input, leaving $this->value = null, which then causes preg_grep() to throw.

A secondary bug was also found in LdapselectField::regex(), which incorrectly used preg_grep() on a plain string value instead of preg_match().

Changes

  • CheckboxesField::deserializeValue(): add is_array() guard after json_decode() so $this->value always ends up as an array, even when the stored data is malformed. This mirrors the existing pattern in ActorField.
  • LdapselectField::regex(): replace preg_grep() with preg_match(), consistent with RadiosField which holds a single string value.
  • tests/CheckboxesField.php: add testRegex() covering valid match, no match, invalid JSON, and JSON-null cases.

@RomainLvr RomainLvr self-assigned this Jun 26, 2026
@RomainLvr
RomainLvr requested a review from stonebuzz June 26, 2026 13:19

@stonebuzz stonebuzz left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@stonebuzz
stonebuzz requested a review from btry June 29, 2026 07:08
@stonebuzz stonebuzz added the bug label Jun 29, 2026
@Rom1-B
Rom1-B self-requested a review July 13, 2026 11:49

public function regex($value): bool {
return (preg_grep($value, $this->value)) ? true : false;
return preg_match($value, $this->value) ? true : false;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

regex() behavior changed (preg_greppreg_match), but tests/3-unit/GlpiPlugin/Formcreator/Field/LdapSelectField.php has no test for regex() at all, before or after this fix. Can you add a case mirroring providerRegex in CheckboxesField.php?

Comment on lines +144 to +147
$decoded = ($value !== null && $value !== '')
? json_decode($value)
: [];
$this->value = is_array($decoded) ? $decoded : [];

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

providerRegex covers null, '', invalid JSON string, and the JSON null literal, but not other valid-JSON-non-array values (e.g. '"a string"', '42', '{"a":1}') that also hit the is_array($decoded) ? $decoded : [] branch. Not required given the generic array check, but worth one more case for full branch coverage.

Comment on lines +621 to +625
yield 'null-decoded json value does not throw and returns false' => [
'value' => 'null',
'pattern' => '/foo/',
'expected' => false,
];

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

providerRegex doesn't cover valid-JSON non-array values (e.g. '"a string"', '42', '{"a":1}') that also reach the is_array($decoded) ? $decoded : [] fallback branch.

Suggested change
yield 'null-decoded json value does not throw and returns false' => [
'value' => 'null',
'pattern' => '/foo/',
'expected' => false,
];
yield 'null-decoded json value does not throw and returns false' => [
'value' => 'null',
'pattern' => '/foo/',
'expected' => false,
];
yield 'valid json non-array value does not throw and returns false' => [
'value' => '"a string"',
'pattern' => '/foo/',
'expected' => false,
];

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

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants