{"record":{"id":"1680a5de8430ca64","repo":"moodle/moodle","slug":"the-value-supplied-was-of-type-type-a-string-was-expected","errorCode":null,"errorMessage":"The value supplied was of type '{$type}'. A string was expected.","messagePattern":"The value supplied was of type '(.+?)'\\. A string was expected\\.","errorType":"validation","errorClass":"TypeError","httpStatus":null,"severity":"error","filePath":"public/lib/table/classes/local/filter/string_filter.php","lineNumber":53,"sourceCode":" * @package    core\n * @copyright  2020 Simey Lameze <simey@moodle.com>\n * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later\n */\nclass string_filter extends filter {\n    /**\n     * Add a value to the filter.\n     *\n     * @param string $values\n     * @return self\n     */\n    public function add_filter_value($value): parent {\n        if (!is_string($value)) {\n            $type = gettype($value);\n            if ($type === 'object') {\n                $type = get_class($value);\n            }\n\n            throw new TypeError(\"The value supplied was of type '{$type}'. A string was expected.\");\n        }\n\n        if (array_search($value, $this->filtervalues) !== false) {\n            // Remove duplicates.\n            return $this;\n        }\n\n        $this->filtervalues[] = $value;\n\n        return $this;\n    }\n}\n","sourceCodeStart":35,"sourceCodeEnd":66,"githubUrl":"https://github.com/moodle/moodle/blob/48ea8c33227277e916cdd4079dbecef448b37f2c/public/lib/table/classes/local/filter/string_filter.php#L35-L66","documentation":"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.","triggerScenarios":"calling add_filter()/setValues() on a string_filter with an int, float, null, bool, array or object value.","commonSituations":"passing integer IDs without casting; null from an unset form field; array values from multi-select inputs; objects from value holders.","solutions":["Cast the value to string with (string)$value before passing it","Guard with is_string() and skip or coerce non-string values","Fix the data source so it returns null-coalesced strings, e.g. $value ?? ''","For arrays of values, array_map('strval', $values) before setValues()"],"exampleFix":"// before\n$filter->setValues([$record->name]);\n// after\n$name = isset($record->name) ? (string)$record->name : '';\n$filter->setValues([$name]);","handlingStrategy":"type-guard","validationCode":"if (!is_string($value)) { $value = (string)$value; }","typeGuard":"function assertString($v): string { if (!is_string($v)) { throw new InvalidArgumentException('Expected string, got ' . (is_object($v) ? get_class($v) : gettype($v))); } return $v; }","tryCatchPattern":"try { $filter->setValues([$value]); } catch (TypeError $e) { $filter->setValues([(string)$value]); }","preventionTips":["Cast IDs and text fields to string before building filters","Use strict_types in filter-consuming code to catch issues early","Null-coalesce optional inputs: $input ?? ''"],"tags":["php","type-error","table-filter","string"],"backgroundTag":"type-mismatch","analyzedSha":"48ea8c33227277e916cdd4079dbecef448b37f2c","analyzedAt":"2026-09-14T20:34:48.527Z","contentChangedAt":"2026-09-14T20:34:48.527Z","schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}