elkekas
New Member
Subject: Bug in form.php (line 1798) when counting non-array data in repeat groups
Issue Description:
There is an issue in components/com_fabrik/models/form.php at line 1798, where the code assumes that the value retrieved from $this->formData[$smallerElHTMLName] is always an array. However, in some cases, the value can be a string or even null. This leads to a TypeError in PHP 8+ because count() can only be used on arrays or objects implementing the Countable interface.
Problematic Code (line 1798):
If $this->formData[$smallerElHTMLName] is not an array, this will trigger an error in PHP 8:
TypeError: count(): Argument #1 ($value) must be of type Countable|array, string given
Proposed Solution:
Add a validation to check whether $this->formData[$smallerElHTMLName] is an array before calling count(). If it is not an array, handle it as a single value or consider it empty if the value is null or an empty string.
Corrected Code:
Explanation of Fix:
File: components/com_fabrik/models/form.php
Line: 1798
This fix ensures compatibility with PHP 8+ and avoids errors when handling non-array values in repeat groups.
Issue Description:
There is an issue in components/com_fabrik/models/form.php at line 1798, where the code assumes that the value retrieved from $this->formData[$smallerElHTMLName] is always an array. However, in some cases, the value can be a string or even null. This leads to a TypeError in PHP 8+ because count() can only be used on arrays or objects implementing the Countable interface.
Problematic Code (line 1798):
Code:
$repeatGroup = count($this->formData[$smallerElHTMLName]);
TypeError: count(): Argument #1 ($value) must be of type Countable|array, string given
Proposed Solution:
Add a validation to check whether $this->formData[$smallerElHTMLName] is an array before calling count(). If it is not an array, handle it as a single value or consider it empty if the value is null or an empty string.
Corrected Code:
Code:
if (isset($this->formData[$smallerElHTMLName])) {
$fieldData = $this->formData[$smallerElHTMLName];
if (is_array($fieldData)) {
$repeatGroup = count($fieldData);
} else {
$repeatGroup = !empty($fieldData) ? 1 : 0; // 1 if data exists, 0 if empty
}
} else {
$repeatGroup = 0; // Default if the key does not exist
}
- Use isset() to ensure the key exists in $this->formData.
- Check if the value is an array using is_array().
- For non-array values, check if the value is non-empty (!empty()) to decide whether to consider it as one repetition (1) or none (0).
File: components/com_fabrik/models/form.php
Line: 1798
This fix ensures compatibility with PHP 8+ and avoids errors when handling non-array values in repeat groups.