{"record":{"id":"5d8026355ec3b3eb","repo":"symfony/routing","slug":"case-must-be-a-backedenum-instance-s-given","errorCode":null,"errorMessage":"Case must be a \"BackedEnum\" instance, \"%s\" given.","messagePattern":"Case must be a \"BackedEnum\" instance, \"(.+?)\" given\\.","errorType":"exception","errorClass":"InvalidArgumentException","httpStatus":null,"severity":"error","filePath":"Requirement/EnumRequirement.php","lineNumber":38,"sourceCode":"    /**\n     * @template T of \\BackedEnum\n     *\n     * @param class-string<T>|list<T> $cases\n     */\n    public function __construct(string|array $cases = [])\n    {\n        if (\\is_string($cases)) {\n            if (!is_subclass_of($cases, \\BackedEnum::class, true)) {\n                throw new InvalidArgumentException(\\sprintf('\"%s\" is not a \"BackedEnum\" class.', $cases));\n            }\n\n            $cases = $cases::cases();\n        } else {\n            $class = null;\n\n            foreach ($cases as $case) {\n                if (!$case instanceof \\BackedEnum) {\n                    throw new InvalidArgumentException(\\sprintf('Case must be a \"BackedEnum\" instance, \"%s\" given.', get_debug_type($case)));\n                }\n\n                $class ??= $case::class;\n\n                if (!$case instanceof $class) {\n                    throw new InvalidArgumentException(\\sprintf('\"%s::%s\" is not a case of \"%s\".', get_debug_type($case), $case->name, $class));\n                }\n            }\n        }\n\n        $this->requirement = implode('|', array_map(static fn ($e) => preg_quote($e->value), $cases));\n    }\n\n    public function __toString(): string\n    {\n        return $this->requirement;\n    }\n}","sourceCodeStart":20,"sourceCodeEnd":56,"githubUrl":"https://github.com/symfony/routing/blob/83fa223250b50f4f018c011e101c330e65ac63cc/Requirement/EnumRequirement.php#L20-L56","documentation":"EnumRequirement accepts either a single enum class-string or an array of enum cases. When an array is passed, every element must be a BackedEnum instance; this error is thrown for any non-backed enum (unit enum) or non-enum value found in the array.","triggerScenarios":"new EnumRequirement(SomeUnitEnum::class) where SomeUnitEnum is a plain (non-backed) enum, or passing an array mixing BackedEnum cases with unit enum cases, plain strings, or other values.","commonSituations":"Developers convert a unit enum to a route requirement (requirements only work with backed enums since they must map to string/int URL values), or accidentally pass enum class-strings mixed with case instances in the array.","solutions":["Back the enum with string or int values (e.g. enum Status: string { case Active = 'active'; }) and pass backed cases or the class-string","Pass a single backed enum class-string instead of an array of cases","Filter the array to contain only BackedEnum instances before constructing the requirement"],"exampleFix":"// before\nenum Status { case Active; case Inactive; }\n$req = new EnumRequirement([Status::Active, Status::Inactive]);\n// after\nenum Status: string { case Active = 'active'; case Inactive = 'inactive'; }\n$req = new EnumRequirement(Status::class);","handlingStrategy":"type-guard","validationCode":"$cases = (array) $input;\nforeach ($cases as $c) {\n    if (!$c instanceof \\BackedEnum) throw new \\InvalidArgumentException('All cases must be BackedEnum instances');\n}","typeGuard":"function isBackedEnumCaseList(array $cases): bool {\n    return array_all($cases, fn($c) => $c instanceof \\BackedEnum);\n}","tryCatchPattern":"try {\n    $requirement = new EnumRequirement($input);\n} catch (\\InvalidArgumentException $e) {\n    // log and skip/fix the enum configuration\n}","preventionTips":["Only use backed enums (string/int) in route requirements","Prefer passing the enum class-string over a hand-built array of cases","Unit-test route requirement construction for every enum used in routes"],"tags":["php","routing","enum","requirements"],"backgroundTag":"invalid-enum-value","analyzedSha":"83fa223250b50f4f018c011e101c330e65ac63cc","analyzedAt":"2026-09-14T03:19:46.280Z","contentChangedAt":"2026-09-14T03:19:46.280Z","schemaVersion":2},"datasetVersion":"2026-09-16T09:17:16.951Z"}