moodle/moodle · error · TypeError

The value supplied was of type

Error message

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

What it means

thrown by the numeric comparison table filter when the supplied filter value is not numeric. PHP's TypeError is raised after gettype() (or get_class() for objects) reports the actual type so the developer can see what was passed instead of a number.

Solutions

  1. Cast or validate the value with is_numeric() before passing it to the filter
  2. Convert numeric strings with (float)$value or (int)$value prior to setValues()
  3. Check the source of the value (form data, DB row) for null/empty defaults and substitute a numeric default
  4. If an object is passed, extract its numeric property instead of passing the object

Example fix

// before
$filter->setValues([$row->score]);
// after
$score = is_numeric($row->score ?? null) ? (float)$row->score : 0;
$filter->setValues([$score]);
Defensive patterns

Strategy: type-guard

Validate before calling

$values = array_map(function ($v) { return is_numeric($v) ? $v + 0 : null; }, $values); if (in_array(null, $values, true)) { throw new InvalidArgumentException('non-numeric filter value'); }

Type guard

function assertNumeric($v): float { if (!is_numeric($v)) { throw new InvalidArgumentException('Expected numeric, got ' . (is_object($v) ? get_class($v) : gettype($v))); } return (float)$v; }

Try / catch

try { $filter->setValues([$value]); } catch (TypeError $e) { // log and skip this filter, or cast and retry
    $filter->setValues([(float)$value]); }

Prevention

When it happens

Trigger: calling add_filter()/setValues() (or similar) on a numeric_comparison_filter with a value that fails is_numeric(): arrays, null, booleans, non-numeric strings, or objects.

Common situations: form input not cast before being passed to the filter; database fields returning null; passing a stdClass value object instead of a scalar; copy-paste from a string filter configuration.

Understand the failure class

Background: "Must be a positive integer", "Invalid value", "Unsupported": the invalid-argument-value error family, when a library rejects the value you pass — this error's family across 35 libraries.

Related errors


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

Appendix: source

Thrown at public/lib/table/classes/local/filter/numeric_comparison_filter.php:116

        }

        if (!property_exists($data, 'direction')) {
            throw new InvalidArgumentException("A 'direction' must be provided.");
        }
        $direction = $this->get_direction($data->direction);

        if (!property_exists($data, 'value')) {
            throw new InvalidArgumentException("A 'value' must be provided.");
        }
        $value = $data->value;

        if (!is_numeric($value)) {
            $type = gettype($value);
            if ($type === 'object') {
                $type = get_class($value);
            }

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

        $fullvalue = (object) [
            'direction' => $direction,
            'value' => $value,
        ];

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

        $this->filtervalues[] = $fullvalue;

        return $this;
    }
}

View on GitHub (pinned to 48ea8c3322)