{"record":{"id":"a10a9974b8ad25de","repo":"PHPOffice/PhpSpreadsheet","slug":"cannot-set-data-validation-for-cell-that-is-not-bo","errorCode":null,"errorMessage":"Cannot set data validation for cell that is not bound to a worksheet","messagePattern":"Cannot set 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":751,"sourceCode":"     */\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\n        return $this->updateInCollection();\n    }\n\n    /**\n     * Does this cell contain valid value?\n     */\n    public function hasValidValue(): bool\n    {\n        $validator = new DataValidator();\n\n        return $validator->isValid($this);\n    }\n\n    /**","sourceCodeStart":733,"sourceCodeEnd":769,"githubUrl":"https://github.com/PHPOffice/PhpSpreadsheet/blob/65b080eef4d9fd11a5796135ab145883e5c3d6a6/src/PhpSpreadsheet/Cell/Cell.php#L733-L769","documentation":"setDataValidation() attaches validation rules to the cell's coordinate on its worksheet, which requires a live parent ($this->parent) to resolve both worksheet and coordinate. On a detached Cell the write has nowhere to land, so the method throws before touching the worksheet.","triggerScenarios":"Calling setDataValidation($dv) on a Cell held after worksheet removal/teardown; applying queued validation objects to cells whose sheet was deleted in between.","commonSituations":"Batch builders that collect (cell, validation) pairs and flush them later; cleanup routines that remove sheets before deferred writes complete.","solutions":["Write through the worksheet by coordinate string: $sheet->setDataValidation('A1', $dv)","Flush pending writes before removing sheets or unsetting the spreadsheet","Guard with $cell->getParent() !== null and skip/requeue detached cells"],"exampleFix":"// before\n$cell->setDataValidation($dv); // throws when detached\n\n// after\n$sheet->setDataValidation('A1', $dv);","handlingStrategy":"validation","validationCode":"if ($cell->getParent() !== null) {\n    $cell->setDataValidation($dv);\n} else {\n    $sheet->setDataValidation($coord, $dv);\n}","typeGuard":"function cellIsBoundToWorksheet(Cell $cell): bool\n{\n    return $cell->getParent() !== null && $cell->getParent()->getParent() !== null;\n}","tryCatchPattern":"null","preventionTips":["Flush queued validation writes before removing sheets or unsetting the spreadsheet","Write validations through Worksheet::setDataValidation($coord, $dv) with coordinate strings","Keep a live reference to the target sheet for the whole write phase"],"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"}