PHPOffice/PhpSpreadsheet · error · PhpOffice\PhpSpreadsheet\Exception
invalid dynamic rule type $dynamicRuleType
Error message
invalid dynamic rule type $dynamicRuleType
What it means
When evaluating a dynamicFilter rule, AutoFilter::dynamicFilterDateRange() looks the rule value up in its private DATE_FUNCTIONS map, which contains only the 17 date-window types (yesterday, today, tomorrow, yearToDate, this/last/next Year|Quarter|Month|Week). Any other value reaching this method throws. Note that aboveAverage/belowAverage and M1-M12/Q1-Q4 codes are valid groupings but are not in this map, so they must not reach the date-range path.
Source
Thrown at src/PhpSpreadsheet/Worksheet/AutoFilter.php:737
private static function dynamicYesterday(): array
{
$maxval = new DateTime();
$maxval->setTime(0, 0, 0);
$val = clone $maxval;
$val->modify('-1 day');
return [$val, $maxval];
}
/**
* Convert a dynamic rule daterange to a custom filter range expression for ease of calculation.
*
* @return mixed[]
*/
private function dynamicFilterDateRange(string $dynamicRuleType, AutoFilter\Column &$filterColumn): array
{
$ruleValues = [];
$callBack = [__CLASS__, self::DATE_FUNCTIONS[$dynamicRuleType] ?? throw new Exception("invalid dynamic rule type $dynamicRuleType")];
// Calculate start/end dates for the required date range based on current date
// Val is lowest permitted value.
// Maxval is greater than highest permitted value
[$val, $maxval] = $callBack();
$val = Date::dateTimeToExcel($val);
$maxval = Date::dateTimeToExcel($maxval);
// Set the filter column rule attributes ready for writing
$filterColumn->setAttributes(['val' => $val, 'maxVal' => $maxval]);
// Set the rules for identifying rows for hide/show
$ruleValues[] = ['operator' => Rule::AUTOFILTER_COLUMN_RULE_GREATERTHANOREQUAL, 'value' => $val];
$ruleValues[] = ['operator' => Rule::AUTOFILTER_COLUMN_RULE_LESSTHAN, 'value' => $maxval];
return ['method' => 'filterTestInCustomDataSet', 'arguments' => ['filterRules' => $ruleValues, 'join' => AutoFilter\Column::AUTOFILTER_COLUMN_JOIN_AND]];
}
/**View on GitHub (pinned to 65b080eef4)
Solutions
- Use the Rule::AUTOFILTER_RULETYPE_DYNAMIC_* constants (e.g. Rule::AUTOFILTER_RULETYPE_DYNAMIC_LASTMONTH) instead of raw strings
- Validate the value against the 17 known date-window tokens before applying or evaluating the filter
- Reject or sanitize dynamicFilter values coming from untrusted files before re-saving/evaluating
Example fix
// before
$rule->setRuleType(Rule::AUTOFILTER_RULETYPE_DYNAMICFILTER)->setValue('lastFortnight');
// after
$rule->setRuleType(Rule::AUTOFILTER_RULETYPE_DYNAMICFILTER)->setValue(Rule::AUTOFILTER_RULETYPE_DYNAMIC_LASTWEEK); Defensive patterns
Strategy: validation
Validate before calling
$dateWindowTypes = [
'yesterday', 'today', 'tomorrow', 'yearToDate',
'thisYear', 'thisQuarter', 'thisMonth', 'thisWeek',
'lastYear', 'lastQuarter', 'lastMonth', 'lastWeek',
'nextYear', 'nextQuarter', 'nextMonth', 'nextWeek',
];
if (!in_array($dynamicValue, $dateWindowTypes, true)) {
throw new InvalidArgumentException("Unknown dynamic filter '$dynamicValue'");
} Type guard
function isDateWindowDynamicType(string $value): bool
{
return in_array($value, [
'yesterday', 'today', 'tomorrow', 'yearToDate',
'thisYear', 'thisQuarter', 'thisMonth', 'thisWeek',
'lastYear', 'lastQuarter', 'lastMonth', 'lastWeek',
'nextYear', 'nextQuarter', 'nextMonth', 'nextWeek',
], true);
} Try / catch
try {
$autoFilter->showHideRows();
} catch (\PhpOffice\PhpSpreadsheet\Exception $e) {
// a dynamic rule value is not in the date-window map: strip dynamic rules and re-evaluate
} Prevention
- Always set dynamic values via Rule::AUTOFILTER_RULETYPE_DYNAMIC_* constants
- Validate untrusted filter definitions before applying them to a worksheet
When it happens
Trigger: Building a Rule with AUTOFILTER_RULETYPE_DYNAMICFILTER and a made-up value like 'lastFortnight', then running $autoFilter->showHideRows(); also loaded files whose dynamicFilter val attribute does not match a known token.
Common situations: Hand-constructed dynamic rules with invented strings; user input used directly as a dynamic filter value; malformed/untrusted Xlsx files.
Related errors
- Invalid filter type for column AutoFilter.
- Invalid rule connection for column AutoFilter.
- Invalid rule type for column AutoFilter Rule.
- Invalid operator for column AutoFilter Rule.
- Invalid grouping for column AutoFilter Rule.
AI-assisted analysis of PHPOffice/PhpSpreadsheet@65b080eef4 (2026-08-17).
Data as JSON: /api/errors/b3503a216a9c390b.
Report an issue: GitHub.