{"record":{"id":"ac2a6e350c0ba244","repo":"phalcon/cphalcon","slug":"a-dependency-injection-container-is-required-to-ac-ac2a6e","errorCode":null,"errorMessage":"A dependency injection container is required to access the 'filter' service","messagePattern":"A dependency injection container is required to access the 'filter' service","errorType":"exception","errorClass":"Phalcon\\Filter\\Validation\\Exceptions\\FilterServiceUnavailable","httpStatus":null,"severity":"error","filePath":"phalcon/Filter/Validation.zep","lineNumber":203,"sourceCode":"     */\n    public function bind(var entity, var data, array whitelist = []) -> <static>\n    {\n        var container, field, value, fieldFilters, filterService, filters, method;\n\n        let this->data = data;\n        this->setEntity(entity);\n\n        // if data is not an array / object, entity is null, or data is empty, then no need to proceed further\n        if unlikely (typeof data != \"array\" && typeof data != \"object\") || (null === entity) || empty data {\n            return this;\n        }\n\n        let container = this->getDI();\n        if container === null {\n            let container = Di::getDefault();\n\n            if container === null {\n                throw new FilterServiceUnavailable();\n            }\n        }\n        let filterService = <FilterInterface> container->getShared(\"filter\");\n        if unlikely typeof filterService != \"object\" {\n            throw new InvalidFilterService();\n        }\n\n        if empty whitelist {\n            let whitelist = this->whitelist;\n        }\n\n        let filters = this->filters;\n\n        for field, value in data {\n            /**\n             * Skip numeric (integer) keys; entity setters and properties are\n             * always string-named, so camelize() would fail on them. See\n             * cphalcon issue #17173.","sourceCodeStart":185,"sourceCodeEnd":221,"githubUrl":"https://github.com/phalcon/cphalcon/blob/b7419de9cd0a8a3f48441ead84c9f8415d463e25/phalcon/Filter/Validation.zep#L185-L221","documentation":"Validation::bind() pre-sanitizes incoming data through the DI 'filter' service, so it needs a service container. FilterServiceUnavailable is thrown when the validation object has no container injected (getDI() is null) and the global default container Di::getDefault() is also null — i.e., the validation runs standalone with no DI anywhere.","triggerScenarios":"$v = new Validation(); $v->bind($entity, $data); in a plain PHP script, unit test, or queue worker where no FactoryDefault was ever created; calling bind() before Di::setDefault() runs; Di::reset() in a test tearDown leaving the default container null while a later bind() still executes.","commonSituations":"Using Phalcon\\Filter\\Validation outside a full Phalcon application (micro-service, CLI script, PHPUnit); long-running workers started before the DI was bootstrapped; tests that reset DI state between cases.","solutions":["Set a container on the validation: $validation->setDI(new FactoryDefault()) (or your existing $di)","Or establish the global default once at bootstrap: Di::setDefault($di)","Ensure the container actually defines a 'filter' service once the container issue is fixed (next error will be FilterServiceUnavailable/InvalidFilterService otherwise)"],"exampleFix":"// before\n$v = new Validation();\n$v->bind($entity, $_POST); // throws FilterServiceUnavailable: no DI anywhere\n\n// after\n$di = new FactoryDefault();\nDi::setDefault($di);\n$v = new Validation();\n$v->setDI($di);\n$v->bind($entity, $_POST);","handlingStrategy":"validation","validationCode":"if ($validation->getDI() === null && Di::getDefault() === null) {\n    throw new RuntimeException('Validation needs a DI container with a filter service; call setDI() or Di::setDefault()');\n}\n$validation->bind($entity, $data);","typeGuard":"function hasFilterContainer(\\Phalcon\\Filter\\Validation $v): bool\n{\n    return $v->getDI() !== null || \\Phalcon\\Di\\Di::getDefault() !== null;\n}","tryCatchPattern":"use Phalcon\\Filter\\Validation\\Exceptions\\FilterServiceUnavailable;\ntry {\n    $v->bind($entity, $data);\n} catch (FilterServiceUnavailable $e) {\n    $v->setDI(new FactoryDefault()); // bootstrap on demand in workers/CLI\n    $v->bind($entity, $data);\n}","preventionTips":["Call Di::setDefault($di) once at process start — CLI scripts, queue workers and tests included","Inject the container into every standalone Validation: setDI($di)","In long-running workers, re-establish the container after any Di::reset()"],"tags":["phalcon","validation","dependency-injection","service-container"],"backgroundTag":"missing-di-container","analyzedSha":"b7419de9cd0a8a3f48441ead84c9f8415d463e25","analyzedAt":"2026-08-21T06:21:18.811Z","schemaVersion":2},"datasetVersion":"2026-08-21T11:28:35.574Z"}