PHPOffice/PhpSpreadsheet · error · SpreadsheetException
Cannot set data validation for cell that is not bound to a w
Error message
Cannot set data validation for cell that is not bound to a worksheet
What it means
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.
Source
Thrown at src/PhpSpreadsheet/Cell/Cell.php:751
*/
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());
}
/**
* Set Data validation rules.
*
* @throws SpreadsheetException
*/
public function setDataValidation(?DataValidation $dataValidation = null): self
{
if (!isset($this->parent)) {
throw new SpreadsheetException('Cannot set data validation for cell that is not bound to a worksheet');
}
$this->getWorksheet()->setDataValidation($this->getCoordinate(), $dataValidation);
return $this->updateInCollection();
}
/**
* Does this cell contain valid value?
*/
public function hasValidValue(): bool
{
$validator = new DataValidator();
return $validator->isValid($this);
}
/**View on GitHub (pinned to 65b080eef4)
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
Example fix
// before
$cell->setDataValidation($dv); // throws when detached
// after
$sheet->setDataValidation('A1', $dv); Defensive patterns
Strategy: validation
Validate before calling
if ($cell->getParent() !== null) {
$cell->setDataValidation($dv);
} else {
$sheet->setDataValidation($coord, $dv);
} Type guard
function cellIsBoundToWorksheet(Cell $cell): bool
{
return $cell->getParent() !== null && $cell->getParent()->getParent() !== null;
} Try / catch
null
Prevention
- 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
When it happens
Trigger: Calling setDataValidation($dv) on a Cell held after worksheet removal/teardown; applying queued validation objects to cells whose sheet was deleted in between.
Common situations: Batch builders that collect (cell, validation) pairs and flush them later; cleanup routines that remove sheets before deferred writes complete.
Related errors
- Cannot check for data validation when cell is not bound to a
- Cannot get 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/a10a9974b8ad25de.
Report an issue: GitHub.