PHPOffice/PhpSpreadsheet · error · SpreadsheetException

Cannot get data validation for cell that is not bound to a w

Error message

Cannot get data validation for cell that is not bound to a worksheet

What it means

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.

Source

Thrown at src/PhpSpreadsheet/Cell/Cell.php:737

     */
    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());
    }

    /**
     * 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);

View on GitHub (pinned to 65b080eef4)

Solutions

  1. Query the live worksheet by coordinate string instead: $sheet->getDataValidation('A1')
  2. Guard with $cell->getParent() !== null before the call
  3. Keep the coordinate string (not the Cell) as the cache key and refetch cells on demand

Example fix

// before
$dv = $cell->getDataValidation(); // throws when detached

// after
$dv = $sheet->getDataValidation('A1'); // worksheet API takes a coordinate string
Defensive patterns

Strategy: validation

Validate before calling

if ($cell->getParent() !== null) {
    $dv = $cell->getDataValidation();
} else {
    $dv = $sheet->getDataValidation($coord); // coordinate-string API
}

Type guard

function cellIsBoundToWorksheet(Cell $cell): bool
{
    return $cell->getParent() !== null && $cell->getParent()->getParent() !== null;
}

Try / catch

null

Prevention

When it happens

Trigger: Calling getDataValidation() on a Cell kept after its worksheet was removed or the spreadsheet unset; reusing Cell references captured in an earlier processing stage.

Common situations: Template-stamping code that reads validation rules from source cells after the source sheet was discarded; long-running workers caching Cell objects between jobs.

Related errors


AI-assisted analysis of PHPOffice/PhpSpreadsheet@65b080eef4 (2026-08-17). Data as JSON: /api/errors/7d97155dbe553243. Report an issue: GitHub.