{"record":{"id":"db037cbf86da66f0","repo":"PHPOffice/PhpSpreadsheet","slug":"cannot-check-for-data-validation-when-cell-is-not","errorCode":null,"errorMessage":"Cannot check for data validation when cell is not bound to a worksheet","messagePattern":"Cannot check for data validation when cell is not bound to a worksheet","errorType":"exception","errorClass":"SpreadsheetException","httpStatus":null,"severity":"error","filePath":"src/PhpSpreadsheet/Cell/Cell.php","lineNumber":723,"sourceCode":"    }\n\n    /**\n     * Identify if the cell contains a formula.\n     */\n    public function isFormula(): bool\n    {\n        return $this->dataType === DataType::TYPE_FORMULA && $this->getStyle()->getQuotePrefix() === false;\n    }\n\n    /**\n     *    Does this cell contain Data validation rules?\n     *\n     * @throws SpreadsheetException\n     */\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    }","sourceCodeStart":705,"sourceCodeEnd":741,"githubUrl":"https://github.com/PHPOffice/PhpSpreadsheet/blob/65b080eef4d9fd11a5796135ab145883e5c3d6a6/src/PhpSpreadsheet/Cell/Cell.php#L705-L741","documentation":"hasDataValidation() first requires the cell to still be bound to a worksheet ($this->parent set); only then does it delegate to Worksheet::dataValidationExists($coordinate). A detached Cell - one whose collection/worksheet is gone - cannot answer worksheet-level questions, so the call throws before any validation lookup happens.","triggerScenarios":"Calling hasDataValidation() on a Cell retained after removeSheetByIndex()/unset($spreadsheet); on cells fetched from a worksheet that was later detached from its spreadsheet.","commonSituations":"Post-processing pipelines (e.g. report validators) that receive Cell objects harvested earlier; event listeners firing during teardown that still touch cell state.","solutions":["Guard with $cell->getParent() !== null (or getWorksheetOrNull() !== null) before calling","Query the worksheet directly with a coordinate string: $sheet->dataValidationExists($coord) - no Cell object needed","Refetch the cell from a live worksheet when you still have the coordinate"],"exampleFix":"// before\n$ok = $cell->hasDataValidation(); // throws when detached\n\n// after\n$ok = $sheet !== null && $sheet->dataValidationExists('A1');","handlingStrategy":"validation","validationCode":"if ($sheet !== null) {\n    $has = $sheet->dataValidationExists($coord);\n} elseif ($cell->getParent() !== null) {\n    $has = $cell->hasDataValidation();\n} else {\n    $has = false;\n}","typeGuard":"function canQueryCellState(Cell $cell): bool\n{\n    return $cell->getParent() !== null;\n}","tryCatchPattern":"null","preventionTips":["Prefer worksheet-level checks with coordinate strings over Cell-object checks","Do not hold Cell references past sheet removal or spreadsheet teardown","Centralize cell-state queries behind a helper that guards getParent() first"],"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"}