{"record":{"id":"8d5957902550da2e","repo":"symfony/routing","slug":"routing-requirement-for-s-cannot-be-empty","errorCode":null,"errorMessage":"Routing requirement for \"%s\" cannot be empty.","messagePattern":"Routing requirement for \"(.+?)\" cannot be empty\\.","errorType":"exception","errorClass":"InvalidArgumentException","httpStatus":null,"severity":"error","filePath":"Route.php","lineNumber":477,"sourceCode":"\n    private function sanitizeRequirement(string $key, string $regex): string\n    {\n        if ('' !== $regex) {\n            if ('^' === $regex[0]) {\n                $regex = substr($regex, 1);\n            } elseif (str_starts_with($regex, '\\\\A')) {\n                $regex = substr($regex, 2);\n            }\n        }\n\n        if (str_ends_with($regex, '$')) {\n            $regex = substr($regex, 0, -1);\n        } elseif (\\strlen($regex) - 2 === strpos($regex, '\\\\z')) {\n            $regex = substr($regex, 0, -2);\n        }\n\n        if ('' === $regex) {\n            throw new \\InvalidArgumentException(\\sprintf('Routing requirement for \"%s\" cannot be empty.', $key));\n        }\n\n        return $regex;\n    }\n\n    private function isLocalized(): bool\n    {\n        return isset($this->defaults['_locale']) && isset($this->defaults['_canonical_route']) && ($this->requirements['_locale'] ?? null) === preg_quote($this->defaults['_locale']);\n    }\n}\n","sourceCodeStart":459,"sourceCodeEnd":488,"githubUrl":"https://github.com/symfony/routing/blob/83fa223250b50f4f018c011e101c330e65ac63cc/Route.php#L459-L488","documentation":"Route::sanitizeRequirement() strips a leading '^' and trailing '$' (or '\\z') anchor from requirement regexes, then throws this InvalidArgumentException if nothing is left — i.e. an empty requirement like '' or '^$' would match only empty strings or nothing useful and is rejected.","triggerScenarios":"->setRequirement('id', '') or ->assert('id', '^$'); a variable interpolated into the requirement that is empty at runtime (e.g. sprintf('{%s}', $pattern) with $pattern = ''); config where requirements: { id: '' }.","commonSituations":"Building requirement patterns dynamically from env vars/parameters that resolve to empty strings; YAML config with requirements: { id: ~ } coercing oddly; copying patterns and stripping anchors leaving the empty string.","solutions":["Provide a non-empty regex, e.g. requirements: { id: '\\d+' }","Check any variable interpolated into the requirement is non-empty before building the route","Remove the requirement key entirely if no constraint is needed"],"exampleFix":"// before\n$route->setRequirement('id', '');\n// after\n$route->setRequirement('id', '\\d+');","handlingStrategy":"validation","validationCode":"foreach ($requirements as $k => $r) { if (!is_string($r) || '' === trim($r, '^$\\')) { throw new \\InvalidArgumentException(\"Requirement '$k' must be a non-empty regex\"); } }","typeGuard":"function isValidRequirement(mixed $r): bool { return is_string($r) && '' !== trim($r, \"^$\\z\"); }","tryCatchPattern":"try { $route->addRequirements($requirements); } catch (\\InvalidArgumentException $e) { if (str_contains($e->getMessage(), 'cannot be empty')) { /* drop or fix the empty requirement */ } throw $e; }","preventionTips":["Never interpolate possibly-empty variables into requirement regexes","Remove requirement keys instead of setting them to empty strings","Unit-test route-building code with default parameter values that could be empty"],"tags":["routing","regex","validation","symfony"],"backgroundTag":"empty-required-field","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"}