moodle/moodle · error · TypeError

The value supplied was of type

Error message

The value supplied was of type '{$type}'. A string was expected.

What it means

Thrown by the Moodle table filter class string_filter::add_filter_value() when the value passed to a string-type column filter is not a PHP string. The method's signature accepts anything but immediately type-checks; gettype() (or get_class() for objects) names the offending input, e.g. an array, integer or stdClass passed where the filter API requires a string. This is a strict type guard protecting the SQL filter value composition that follows.

Solutions

  1. Cast the value to string with (string)$value before passing it
  2. Guard with is_string() and skip or coerce non-string values
  3. Fix the data source so it returns null-coalesced strings, e.g. $value ?? ''
  4. For arrays of values, array_map('strval', $values) before setValues()

Example fix

// before
$filter->setValues([$record->name]);
// after
$name = isset($record->name) ? (string)$record->name : '';
$filter->setValues([$name]);
Defensive patterns

Strategy: type-guard

Validate before calling

if (!is_string($value)) { $value = (string)$value; }

Type guard

function assertString($v): string { if (!is_string($v)) { throw new InvalidArgumentException('Expected string, got ' . (is_object($v) ? get_class($v) : gettype($v))); } return $v; }

Try / catch

try { $filter->setValues([$value]); } catch (TypeError $e) { $filter->setValues([(string)$value]); }

Prevention

When it happens

Trigger: calling add_filter()/setValues() on a string_filter with an int, float, null, bool, array or object value.

Common situations: passing integer IDs without casting; null from an unset form field; array values from multi-select inputs; objects from value holders.

Understand the failure class

Background: Type mismatch errors: IllegalArgumentException, TypeError and type guards across 150 open-source libraries — this error's family across 150 libraries.

Related errors


AI-assisted analysis of moodle/moodle@48ea8c3322 (2026-09-14). Data as JSON: /api/errors/1680a5de8430ca64. Report an issue: GitHub.

Appendix: source

Thrown at public/lib/table/classes/local/filter/string_filter.php:53

 * @package    core
 * @copyright  2020 Simey Lameze <simey@moodle.com>
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
 */
class string_filter extends filter {
    /**
     * Add a value to the filter.
     *
     * @param string $values
     * @return self
     */
    public function add_filter_value($value): parent {
        if (!is_string($value)) {
            $type = gettype($value);
            if ($type === 'object') {
                $type = get_class($value);
            }

            throw new TypeError("The value supplied was of type '{$type}'. A string was expected.");
        }

        if (array_search($value, $this->filtervalues) !== false) {
            // Remove duplicates.
            return $this;
        }

        $this->filtervalues[] = $value;

        return $this;
    }
}

View on GitHub (pinned to 48ea8c3322)