PHPOffice/PHPWord · error · InvalidArgumentException
Invalid style value: Options
Error message
Invalid style value: {$value} Options: What it means
AbstractStyle::setEnumVal validates that a style value is one of the allowed enumerated options for that style property; if the value is non-empty and not in the enum, it throws InvalidArgumentException listing the valid options. It is the generic enum validator for all PhpWord styles.
Solutions
- Use the style class's constants (e.g. Tab::TAB_STOP_TYPE_CENTER) instead of literal strings
- Check the exception message for the exact list of accepted options and correct the value
- Normalize user input (trim, lowercase) and map it to a valid enum value before calling the setter
Example fix
// before
$tab->setType('middle');
// after
$tab->setType(Tab::TAB_STOP_TYPE_CENTER); // 'center' Defensive patterns
Strategy: validation
Validate before calling
$allowed = ['clear','left','center','right','decimal','bar','num'];
if (!in_array($value, $allowed, true)) {
throw new ValueError("Tab type must be one of: " . implode(',', $allowed));
}
$tab->setType($value); Try / catch
try {
$style->setType($value);
} catch (\InvalidArgumentException $e) {
// fall back to a safe default enum value
$style->setType($style::DEFAULT_CONSTANT);
} Prevention
- Always use the class constants instead of string literals
- Normalize/trim user input before passing to style setters
- Write a unit test per style property enumerating accepted values
When it happens
Trigger: Setting a style property via setters like setType, setPos, setHPos, setVPos, setHPosRelTo, setVPosRelTo with a string not present in the style's allowed constants, e.g. (new Tab())->setType('middle') when only 'clear','left','center','right','decimal','bar','num' are accepted.
Common situations: Typo in style constant ('Center' vs 'center'); copying values from OOXML documentation that are not supported by PhpWord; case-sensitivity issues; passing user input directly into style setters.
Understand the failure class
Background: Invalid enum value errors: "Unknown type", "Invalid scope", "must be one of" — when a string is not on the library's allowed list — this error's family across 23 libraries.
Related errors
- Invalid style value
- Line height must be a valid number
- Invalid value, on of ' . implode(', ', $position) . '…
- Invalid value, on of ' . implode(', ', $restartNumbers) . '…
- Invalid value, dirty or clean possible
AI-assisted analysis of PHPOffice/PHPWord@aef95c0415 (2026-09-14).
Data as JSON: /api/errors/d4f525e4ccaed1e8.
Report an issue: GitHub.
Appendix: source
Thrown at src/PhpWord/Style/AbstractStyle.php:308
$value = $default;
}
return $value;
}
/**
* Set enum value.
*
* @param mixed $value
* @param array $enum
* @param mixed $default
*
* @return mixed
*/
protected function setEnumVal($value = null, $enum = [], $default = null)
{
if ($value != null && trim($value) != '' && !empty($enum) && !in_array($value, $enum)) {
throw new InvalidArgumentException("Invalid style value: {$value} Options:" . implode(',', $enum));
} elseif ($value === null || trim($value) == '') {
$value = $default;
}
return $value;
}
/**
* Set object value.
*
* @param mixed $value
* @param mixed &$style
*
* @return mixed
*/
protected function setObjectVal($value, string $styleName, &$style)
{
$styleClass = substr(static::class, 0, (int) strrpos(static::class, '\\')) . '\\' . $styleName;View on GitHub (pinned to aef95c0415)