{"record":{"id":"73409ad48210cd85","repo":"getgrav/grav","slug":"method-object-should-be-instance-of-flexo","errorCode":null,"errorMessage":"__METHOD__(): 'object' should be instance of FlexObjectInterface","messagePattern":"__METHOD__\\(\\): 'object' should be instance of FlexObjectInterface","errorType":"exception","errorClass":"RuntimeException","httpStatus":400,"severity":"error","filePath":"system/src/Grav/Framework/Flex/FlexForm.php","lineNumber":76,"sourceCode":"    private $submitMethod;\n\n    /**\n     * @param array $options    Options to initialize the form instance:\n     *                          (string) name: Form name, allows you to use custom form.\n     *                          (string) unique_id: Unique id for this form instance.\n     *                          (array) form: Custom form fields.\n     *                          (FlexObjectInterface) object: Object instance.\n     *                          (string) key: Object key, used only if object instance isn't given.\n     *                          (FlexDirectory) directory: Flex Directory, mandatory if object isn't given.\n     *\n     * @return FlexFormInterface\n     */\n    public static function instance(array $options = [])\n    {\n        if (isset($options['object'])) {\n            $object = $options['object'];\n            if (!$object instanceof FlexObjectInterface) {\n                throw new RuntimeException(__METHOD__ . \"(): 'object' should be instance of FlexObjectInterface\", 400);\n            }\n        } elseif (isset($options['directory'])) {\n            $directory = $options['directory'];\n            if (!$directory instanceof FlexDirectory) {\n                throw new RuntimeException(__METHOD__ . \"(): 'directory' should be instance of FlexDirectory\", 400);\n            }\n            $key = $options['key'] ?? '';\n            $object = $directory->getObject($key) ?? $directory->createObject([], $key);\n        } else {\n            throw new RuntimeException(__METHOD__ . \"(): You need to pass option 'directory' or 'object'\", 400);\n        }\n\n        $name = $options['name'] ?? '';\n\n        // There is no reason to pass object and directory.\n        unset($options['object'], $options['directory']);\n\n        return $object->getForm($name, $options);","sourceCodeStart":58,"sourceCodeEnd":94,"githubUrl":"https://github.com/getgrav/grav/blob/6040efed04efa69b8209448ed81308e7c24147c2/system/src/Grav/Framework/Flex/FlexForm.php#L58-L94","documentation":"FlexForm::instance() accepts an 'object' option that must implement FlexObjectInterface; anything else (a plain object, an array, a legacy Page) triggers this 400 RuntimeException. The factory delegates form creation to the object itself, so it must be a real Flex object. Use the 'directory'+'key' path instead when you do not have an object instance.","triggerScenarios":"Passing a legacy Grav Page (PageInterface) or stdClass/array where a FlexObject is expected; passing an object from a different abstraction layer (e.g. a Doctrine entity); passing an uninitialized FlexObjectInterface mock in tests.","commonSituations":"Porting legacy page code to Flex Pages and reusing $page variables in FlexForm::instance(); plugin code receiving 'object' from a request/JSON payload instead of loading it from the directory.","solutions":["Load a proper Flex object first: $object = $grav['flex']->getDirectory('pages')->getObject($key); then pass it.","If you only know type+key, use FlexForm::instance(['directory' => $directory, 'key' => $key]) instead of 'object'.","For legacy Page instances, migrate to the Flex page object via the pages flex directory before building the form."],"exampleFix":"// before\n$form = FlexForm::instance(['object' => $page]); // $page is legacy PageInterface\n\n// after\n$flex = Grav::instance()['flex'];\n$object = $flex->getDirectory('pages')->getObject($page->route());\n$form = FlexForm::instance(['object' => $object]);","handlingStrategy":"type-guard","validationCode":null,"typeGuard":"use Grav\\Framework\\Flex\\Interfaces\\FlexObjectInterface;\n\nfunction isFlexObject(mixed $value): bool\n{\n    return $value instanceof FlexObjectInterface;\n}","tryCatchPattern":null,"preventionTips":["Type-hint parameters against FlexObjectInterface in your own APIs so misuse fails earlier.","Convert legacy Page objects via the flex pages directory before building forms.","When in doubt, use the 'directory'+'key' options — they resolve the object for you."],"tags":["grav","flex","forms","type-check","invalid-argument"],"backgroundTag":"invalid-argument-type","analyzedSha":"6040efed04efa69b8209448ed81308e7c24147c2","analyzedAt":"2026-08-17T05:07:31.593Z","schemaVersion":2},"datasetVersion":"2026-08-17T09:17:11.063Z"}