{"record":{"id":"eb08f48b780bf501","repo":"PHPOffice/PhpSpreadsheet","slug":"no-more-than-2-rules-are-allowed-in-a-custom-filte","errorCode":null,"errorMessage":"No more than 2 rules are allowed in a Custom Filter","messagePattern":"No more than 2 rules are allowed in a Custom Filter","errorType":"exception","errorClass":"PhpOffice\\PhpSpreadsheet\\Exception","httpStatus":null,"severity":"error","filePath":"src/PhpSpreadsheet/Worksheet/AutoFilter/Column.php","lineNumber":172,"sourceCode":"     */\n    public function getFilterType(): string\n    {\n        return $this->filterType;\n    }\n\n    /**\n     * Set AutoFilter Type.\n     *\n     * @return $this\n     */\n    public function setFilterType(string $filterType): static\n    {\n        $this->setEvaluatedFalse();\n        if (!in_array($filterType, self::$filterTypes)) {\n            throw new PhpSpreadsheetException('Invalid filter type for column AutoFilter.');\n        }\n        if ($filterType === self::AUTOFILTER_FILTERTYPE_CUSTOMFILTER && count($this->ruleset) > 2) {\n            throw new PhpSpreadsheetException('No more than 2 rules are allowed in a Custom Filter');\n        }\n\n        $this->filterType = $filterType;\n\n        return $this;\n    }\n\n    /**\n     * Get AutoFilter Multiple Rules And/Or Join.\n     */\n    public function getJoin(): string\n    {\n        return $this->join;\n    }\n\n    /**\n     * Set AutoFilter Multiple Rules And/Or.\n     *","sourceCodeStart":154,"sourceCodeEnd":190,"githubUrl":"https://github.com/PHPOffice/PhpSpreadsheet/blob/65b080eef4d9fd11a5796135ab145883e5c3d6a6/src/PhpSpreadsheet/Worksheet/AutoFilter/Column.php#L154-L190","documentation":"Switching a column's filter type to 'customFilters' is refused when the column already holds more than two rules, because Excel custom filters support at most two criteria joined by AND/OR. The check happens inside setFilterType() after the type itself has been validated.","triggerScenarios":"Adding 3+ rules via createRule()/addRule() while the column type is not yet customFilters, then calling setFilterType(Column::AUTOFILTER_FILTERTYPE_CUSTOMFILTER).","commonSituations":"Programmatically building several filter rules first and deciding the type afterwards; migrating a many-value filter to a custom filter in one step.","solutions":["Reduce the ruleset to at most 2 rules (deleteRule() or clear rules) before switching the type","Set the filter type first, then add rules — createRule() enforces the same cap going forward","Use filterType 'filters' when you need to match many values"],"exampleFix":"// before\n$column->setFilterType(Column::AUTOFILTER_FILTERTYPE_CUSTOMFILTER); // ruleset already has 3 rules\n\n// after\nwhile (count($column->getRules()) > 2) {\n    $column->deleteRule(0);\n}\n$column->setFilterType(Column::AUTOFILTER_FILTERTYPE_CUSTOMFILTER);","handlingStrategy":"validation","validationCode":"if (count($column->getRules()) > 2) {\n    throw new InvalidArgumentException('Reduce rules to <= 2 before switching to customFilters');\n}\n$column->setFilterType(Column::AUTOFILTER_FILTERTYPE_CUSTOMFILTER);","typeGuard":null,"tryCatchPattern":"try {\n    $column->setFilterType(Column::AUTOFILTER_FILTERTYPE_CUSTOMFILTER);\n} catch (\\PhpOffice\\PhpSpreadsheet\\Exception $e) {\n    while (count($column->getRules()) > 2) {\n        $column->deleteRule(0);\n    }\n    $column->setFilterType(Column::AUTOFILTER_FILTERTYPE_CUSTOMFILTER);\n}","preventionTips":["Set the custom filter type before adding rules","Model the Excel two-criteria limit in your own filter builder UI/API"],"tags":["autofilter","custom-filter","rules","limit","phpspreadsheet"],"backgroundTag":"rule-limit-exceeded","analyzedSha":"65b080eef4d9fd11a5796135ab145883e5c3d6a6","analyzedAt":"2026-08-17T05:40:41.646Z","schemaVersion":2},"datasetVersion":"2026-08-17T09:17:11.063Z"}