{"record":{"id":"7d97155dbe553243","repo":"PHPOffice/PhpSpreadsheet","slug":"cannot-get-data-validation-for-cell-that-is-not-bo","errorCode":null,"errorMessage":"Cannot get data validation for cell that is not bound to a worksheet","messagePattern":"Cannot get data validation for cell that is not bound to a worksheet","errorType":"exception","errorClass":"SpreadsheetException","httpStatus":null,"severity":"error","filePath":"src/PhpSpreadsheet/Cell/Cell.php","lineNumber":737,"sourceCode":"     */\n    public function hasDataValidation(): bool\n    {\n        if (!isset($this->parent)) {\n            throw new SpreadsheetException('Cannot check for data validation when cell is not bound to a worksheet');\n        }\n\n        return $this->getWorksheet()->dataValidationExists($this->getCoordinate());\n    }\n\n    /**\n     * Get Data validation rules.\n     *\n     * @throws SpreadsheetException\n     */\n    public function getDataValidation(): DataValidation\n    {\n        if (!isset($this->parent)) {\n            throw new SpreadsheetException('Cannot get data validation for cell that is not bound to a worksheet');\n        }\n\n        return $this->getWorksheet()->getDataValidation($this->getCoordinate());\n    }\n\n    /**\n     * Set Data validation rules.\n     *\n     * @throws SpreadsheetException\n     */\n    public function setDataValidation(?DataValidation $dataValidation = null): self\n    {\n        if (!isset($this->parent)) {\n            throw new SpreadsheetException('Cannot set data validation for cell that is not bound to a worksheet');\n        }\n\n        $this->getWorksheet()->setDataValidation($this->getCoordinate(), $dataValidation);\n","sourceCodeStart":719,"sourceCodeEnd":755,"githubUrl":"https://github.com/PHPOffice/PhpSpreadsheet/blob/65b080eef4d9fd11a5796135ab145883e5c3d6a6/src/PhpSpreadsheet/Cell/Cell.php#L719-L755","documentation":"getDataValidation() requires the cell to be bound to a worksheet; it then fetches (or lazily creates) the DataValidation object for the cell's coordinate via Worksheet::getDataValidation(). With $this->parent unset the coordinate cannot be resolved and the call throws immediately.","triggerScenarios":"Calling getDataValidation() on a Cell kept after its worksheet was removed or the spreadsheet unset; reusing Cell references captured in an earlier processing stage.","commonSituations":"Template-stamping code that reads validation rules from source cells after the source sheet was discarded; long-running workers caching Cell objects between jobs.","solutions":["Query the live worksheet by coordinate string instead: $sheet->getDataValidation('A1')","Guard with $cell->getParent() !== null before the call","Keep the coordinate string (not the Cell) as the cache key and refetch cells on demand"],"exampleFix":"// before\n$dv = $cell->getDataValidation(); // throws when detached\n\n// after\n$dv = $sheet->getDataValidation('A1'); // worksheet API takes a coordinate string","handlingStrategy":"validation","validationCode":"if ($cell->getParent() !== null) {\n    $dv = $cell->getDataValidation();\n} else {\n    $dv = $sheet->getDataValidation($coord); // coordinate-string API\n}","typeGuard":"function cellIsBoundToWorksheet(Cell $cell): bool\n{\n    return $cell->getParent() !== null && $cell->getParent()->getParent() !== null;\n}","tryCatchPattern":"null","preventionTips":["Use Worksheet::getDataValidation($coordinate) when you already hold the sheet and coordinate","Read template rules before discarding source sheets in your pipeline","Pass (worksheet, coordinate) pairs - not Cell objects - between processing stages"],"tags":["phpspreadsheet","cell","data-validation","detached-object","worksheet"],"backgroundTag":"detached-object-access","analyzedSha":"65b080eef4d9fd11a5796135ab145883e5c3d6a6","analyzedAt":"2026-08-17T05:40:41.646Z","schemaVersion":2},"datasetVersion":"2026-08-17T09:17:11.063Z"}