{"record":{"id":"9d882c4930c7c7cf","repo":"PHPOffice/PHPWord","slug":"invalid-option-optionkey-possible-values-are-implode-this","errorCode":null,"errorMessage":"Invalid option '$optionkey', possible values are ' . implode(', ', $this->fieldsArray[$this->type]['options'])","messagePattern":"Invalid option '\\$optionkey', possible values are ' \\. implode\\(', ', \\$this->fieldsArray\\[\\$this->type\\]\\['options'\\]\\)","errorType":"exception","errorClass":"InvalidArgumentException","httpStatus":null,"severity":"error","filePath":"src/PhpWord/Element/Field.php","lineNumber":253,"sourceCode":"     * Get Field properties.\n     *\n     * @return array\n     */\n    public function getProperties()\n    {\n        return $this->properties;\n    }\n\n    /**\n     * Set Field options.\n     *\n     * @return self\n     */\n    public function setOptions(array $options = [])\n    {\n        foreach (array_keys($options) as $optionkey) {\n            if (!(isset($this->fieldsArray[$this->type]['options'][$optionkey])) && substr($optionkey, 0, 1) !== '\\\\') {\n                throw new InvalidArgumentException(\"Invalid option '$optionkey', possible values are \" . implode(', ', $this->fieldsArray[$this->type]['options']));\n            }\n        }\n        $this->options = array_merge($this->options, $options);\n\n        return $this;\n    }\n\n    /**\n     * Get Field properties.\n     *\n     * @return array\n     */\n    public function getOptions()\n    {\n        return $this->options;\n    }\n\n    /**","sourceCodeStart":235,"sourceCodeEnd":271,"githubUrl":"https://github.com/PHPOffice/PHPWord/blob/aef95c04151b5633cc505f672ddd6a71da900ee1/src/PhpWord/Element/Field.php#L235-L271","documentation":"Field::setOptions() validates each option key against the per-type 'options' list in $fieldsArray, throwing InvalidArgumentException with the list of valid options in the message. Keys starting with a backslash are allowed through (treated as raw field switches). This guards the generated Word field instruction switches.","triggerScenarios":"Calling $field->setOptions(['Bogus' => true]) where 'Bogus' is not in $fieldsArray[$this->type]['options'] and does not start with '\\'.","commonSituations":"Using option names from a different field type, typos in switch names, or hand-rolling switches without the leading backslash escape.","solutions":["Use only option keys defined in Field::$fieldsArray for your field type; the exception message lists them.","Prefix custom/raw switches with a backslash (e.g. '\\h') to bypass the whitelist.","Verify setType() was called first — an empty type yields an empty options whitelist.","Catch InvalidArgumentException when options come from user config."],"exampleFix":"// before\n$field->setOptions(['Bold' => true]);\n// after\n$field->setOptions(['\\b' => true]);","handlingStrategy":"validation","validationCode":"foreach (array_keys($options) as $k) {\n    if (!str_starts_with($k, '\\\\') && !in_array($k, $allowedOptions, true)) {\n        unset($options[$k]);\n    }\n}","typeGuard":"function isRawSwitch(string $key): bool { return $key !== '' && $key[0] === '\\\\'; }","tryCatchPattern":"try { $field->setOptions($opts); } catch (\\InvalidArgumentException $e) { log($e->getMessage()); }","preventionTips":["Prefix custom Word switches with a backslash to bypass the whitelist","Only use option keys listed for the field's type","Read the exception message — it enumerates the valid options"],"tags":["phpword","invalid-argument","field","options"],"backgroundTag":"invalid-enum-value","analyzedSha":"aef95c04151b5633cc505f672ddd6a71da900ee1","analyzedAt":"2026-09-14T10:53:58.933Z","contentChangedAt":"2026-09-14T10:53:58.933Z","schemaVersion":2},"datasetVersion":"2026-09-16T09:17:16.951Z"}