{"record":{"id":"6e7248871f3416d9","repo":"getgrav/grav","slug":"invalid-argument-value","errorCode":null,"errorMessage":"Invalid argument $value","messagePattern":"Invalid argument \\$value","errorType":"exception","errorClass":"InvalidArgumentException","httpStatus":null,"severity":"error","filePath":"system/src/Grav/Framework/Collection/AbstractIndexCollection.php","lineNumber":284,"sourceCode":"        return array_values($this->loadElements($this->entries));\n    }\n\n    /**\n     * {@inheritDoc}\n     */\n    #[\\ReturnTypeWillChange]\n    public function count()\n    {\n        return count($this->entries);\n    }\n\n    /**\n     * {@inheritDoc}\n     */\n    public function set($key, $value)\n    {\n        if (!$this->isAllowedElement($value)) {\n            throw new InvalidArgumentException('Invalid argument $value');\n        }\n\n        $this->entries[$key] = $this->getElementMeta($value);\n    }\n\n    /**\n     * {@inheritDoc}\n     */\n    public function add($element)\n    {\n        if (!$this->isAllowedElement($element)) {\n            throw new InvalidArgumentException('Invalid argument $element');\n        }\n\n        $this->entries[$this->getCurrentKey($element)] = $this->getElementMeta($element);\n\n        return true;\n    }","sourceCodeStart":266,"sourceCodeEnd":302,"githubUrl":"https://github.com/getgrav/grav/blob/6040efed04efa69b8209448ed81308e7c24147c2/system/src/Grav/Framework/Collection/AbstractIndexCollection.php#L266-L302","documentation":"AbstractIndexCollection (base of FlexIndex) stores lightweight element metadata, not the elements themselves; set() therefore requires $value to pass isAllowedElement(). In Flex collections that check is `$value instanceof FlexObject`, so calling set() with anything else (array, scalar, stdClass, other object) throws InvalidArgumentException('Invalid argument $value').","triggerScenarios":"Calling $flexIndex->set($key, ['field' => 'value']) with a raw data array instead of a FlexObject; inserting a plain entity/DTO object from another ORM; reusing code written against ArrayCollection (which accepts anything) against a FlexIndex; set() with the result of a failed lookup (null).","commonSituations":"Custom Flex object types where developers try to seed the index with unhydrated rows; migrating array-based page collection code to Flex collections; plugins that manipulate Flex collections generically without checking the element contract.","solutions":["Insert FlexObject instances only: fetch/create the object first, then $collection->set($key, $object).","If you hold raw data, instantiate the object via the Flex type (e.g. $flexCollection->getObject() pathways or the object's create/update API) before adding.","For arbitrary key=>value storage, use ArrayCollection/ArrayObject instead of an index collection."],"exampleFix":"// before\n$index->set('abc123', ['title' => 'Hello']); // array -> InvalidArgumentException\n\n// after\n$object = $flex->createObject(['title' => 'Hello'], 'abc123');\n$index->set('abc123', $object); // FlexObject passes isAllowedElement()","handlingStrategy":"type-guard","validationCode":null,"typeGuard":"use Grav\\Framework\\Flex\\FlexObject;\n\nfunction isFlexElement(mixed $value): bool\n{\n    return $value instanceof FlexObject;\n}\n\nif (isFlexElement($value)) {\n    $index->set($key, $value);\n} else {\n    // hydrate first or use ArrayCollection for raw data\n}","tryCatchPattern":null,"preventionTips":["Treat FlexIndex as an index of FlexObject instances only — hydrate raw rows before set().","Null-guard chained lookups before inserting their results.","Use ArrayCollection when you must store arbitrary values."],"tags":["grav","flex","collection","type-validation"],"backgroundTag":"collection-type-mismatch","analyzedSha":"6040efed04efa69b8209448ed81308e7c24147c2","analyzedAt":"2026-08-17T05:07:31.593Z","schemaVersion":2},"datasetVersion":"2026-08-17T09:17:11.063Z"}