PHPOffice/PhpSpreadsheet · error · SpreadsheetException
Cannot check for data validation when cell is not bound to a
Error message
Cannot check for data validation when cell is not bound to a worksheet
What it means
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.
Source
Thrown at src/PhpSpreadsheet/Cell/Cell.php:723
}
/**
* Identify if the cell contains a formula.
*/
public function isFormula(): bool
{
return $this->dataType === DataType::TYPE_FORMULA && $this->getStyle()->getQuotePrefix() === false;
}
/**
* Does this cell contain Data validation rules?
*
* @throws SpreadsheetException
*/
public function hasDataValidation(): bool
{
if (!isset($this->parent)) {
throw new SpreadsheetException('Cannot check for data validation when cell is not bound to a worksheet');
}
return $this->getWorksheet()->dataValidationExists($this->getCoordinate());
}
/**
* Get Data validation rules.
*
* @throws SpreadsheetException
*/
public function getDataValidation(): DataValidation
{
if (!isset($this->parent)) {
throw new SpreadsheetException('Cannot get data validation for cell that is not bound to a worksheet');
}
return $this->getWorksheet()->getDataValidation($this->getCoordinate());
}View on GitHub (pinned to 65b080eef4)
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
Example fix
// before
$ok = $cell->hasDataValidation(); // throws when detached
// after
$ok = $sheet !== null && $sheet->dataValidationExists('A1'); Defensive patterns
Strategy: validation
Validate before calling
if ($sheet !== null) {
$has = $sheet->dataValidationExists($coord);
} elseif ($cell->getParent() !== null) {
$has = $cell->hasDataValidation();
} else {
$has = false;
} Type guard
function canQueryCellState(Cell $cell): bool
{
return $cell->getParent() !== null;
} Try / catch
null
Prevention
- 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
When it happens
Trigger: Calling hasDataValidation() on a Cell retained after removeSheetByIndex()/unset($spreadsheet); on cells fetched from a worksheet that was later detached from its spreadsheet.
Common situations: Post-processing pipelines (e.g. report validators) that receive Cell objects harvested earlier; event listeners firing during teardown that still touch cell state.
Related errors
- Cannot get data validation for cell that is not bound to a w
- Cannot set data validation for cell that is not bound to a w
- Cannot update when cell is not bound to a worksheet
- Cannot get column when cell is not bound to a worksheet
- Cannot get row when cell is not bound to a worksheet
AI-assisted analysis of PHPOffice/PhpSpreadsheet@65b080eef4 (2026-08-17).
Data as JSON: /api/errors/db037cbf86da66f0.
Report an issue: GitHub.