{"record":{"id":"eb4545c397f266f5","repo":"getgrav/grav","slug":"method-directory-should-be-instance-of-fl","errorCode":null,"errorMessage":"__METHOD__(): 'directory' should be instance of FlexDirectory","messagePattern":"__METHOD__\\(\\): 'directory' should be instance of FlexDirectory","errorType":"exception","errorClass":"RuntimeException","httpStatus":400,"severity":"error","filePath":"system/src/Grav/Framework/Flex/FlexForm.php","lineNumber":81,"sourceCode":"     *                          (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);\n    }\n\n    /**\n     * FlexForm constructor.\n     * @param string $name","sourceCodeStart":63,"sourceCodeEnd":99,"githubUrl":"https://github.com/getgrav/grav/blob/6040efed04efa69b8209448ed81308e7c24147c2/system/src/Grav/Framework/Flex/FlexForm.php#L63-L99","documentation":"In FlexForm::instance(), when no 'object' is given the 'directory' option must be a FlexDirectory instance; passing the flex type string, an array of options, or any other value throws this 400 RuntimeException. The directory is used to look up or create the object by key, so a raw string cannot work here.","triggerScenarios":"FlexForm::instance(['directory' => 'pages', 'key' => '/blog']) passing the type name; passing a blueprint array or directory configuration array; passing a FlexIndex or collection object instead of the directory.","commonSituations":"First-time flex form integrations where authors assume the type string is accepted; refactoring from $grav['flex']->getDirectory() inline calls to options arrays and forgetting the resolution step.","solutions":["Resolve the string to a FlexDirectory first: $directory = $grav['flex']->getDirectory('pages'); then pass ['directory' => $directory, 'key' => $key].","Check getDirectory() did not return null for unregistered types before calling instance().","If you already have the object, pass 'object' instead and drop 'directory'."],"exampleFix":"// before\n$form = FlexForm::instance(['directory' => 'pages', 'key' => '/about']);\n\n// after\n$flex = Grav::instance()['flex'];\n$form = FlexForm::instance([\n    'directory' => $flex->getDirectory('pages'),\n    'key' => '/about',\n]);","handlingStrategy":"type-guard","validationCode":null,"typeGuard":"use Grav\\Framework\\Flex\\FlexDirectory;\n\nfunction isFlexDirectory(mixed $value): bool\n{\n    return $value instanceof FlexDirectory;\n}","tryCatchPattern":null,"preventionTips":["Centralize directory resolution: $directory = $grav['flex']->getDirectory($type); assert non-null.","Never pass type strings or config arrays as the 'directory' option.","Add instanceof assertions at plugin boundaries that receive form options from external code."],"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"}