PHPOffice/PHPWord · error · InvalidArgumentException
Invalid style value
Error message
Invalid style value: {$value} What it means
AbstractElement::setEnumVal() is the shared enum validator for element style setters (setType, setVAlign, setTextDirection, setVMerge, setUnit, setLegendPosition). If a non-null, non-empty value is given that is not in the allowed enum array, it throws an InvalidArgumentException with 'Invalid style value: {value}'. Null/empty values fall back to the provided default instead of throwing.
Solutions
- Use the class constants for the style (e.g. Cell::VALIGN_CENTER, Image::UNIT_PX) instead of free-form strings
- Check the exact enum values accepted by the specific setter in the element class
- Match casing exactly — the in_array check is case-sensitive
- Let the value be null/empty to get the setter's default instead of guessing
Example fix
// before
$cell->setVAlign('middle');
// after
use PhpOffice\PhpWord\SimpleType\VerticalJc;
$cell->setVAlign(VerticalJc::CENTER); Defensive patterns
Strategy: validation
Validate before calling
use PhpOffice\PhpWord\SimpleType\VerticalJc;
$allowed = [VerticalJc::TOP, VerticalJc::CENTER, VerticalJc::BOTTOM];
if ($value !== null && trim($value) !== '' && !in_array($value, $allowed, true)) {
throw new UnexpectedValueException("vAlign must be one of: " . implode(', ', $allowed));
} Type guard
function isEnumValue(?string $value, array $enum): bool {
return $value === null || trim($value) === '' || in_array($value, $enum, true);
} Try / catch
try {
$cell->setVAlign($value);
} catch (\InvalidArgumentException $e) {
// leave unset so the library default applies
} Prevention
- Use the SimpleType constant classes (VerticalJc, TextDirection, etc.) instead of raw strings
- Remember the check is case-sensitive — match constants exactly
- Pass null/empty to accept the setter default rather than guessing a value
- Keep a mapping layer between user-facing config keys and PhpWord constants
When it happens
Trigger: Calling a style setter like $table->setStyleByArgument / $image->setImageStyle or direct calls such as $cell->setVAlign('middle') when only 'center'/'top'/'bottom' etc. are allowed; setting setType('x') on ListRun/Table styles with wrong values; passing style arrays with invalid keys' values via constructor.
Common situations: Using CSS-like values ('middle' vs 'center') in vAlign; passing integers for Unit where string constants are expected (or vice versa); values ported from raw OOXML attributes with different casing; typo in a style array.
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 value, on of ' . implode(', ', $position) . '…
- Invalid value, on of ' . implode(', ', $restartNumbers) . '…
- Invalid value, dirty or clean possible
- Invalid value, alignments of ' . implode(', '…
- Cannot add in .
AI-assisted analysis of PHPOffice/PHPWord@aef95c0415 (2026-09-14).
Data as JSON: /api/errors/b2e2fc60ba311341.
Report an issue: GitHub.
Appendix: source
Thrown at src/PhpWord/Element/AbstractElement.php:537
{
$this->trackChange = new TrackChange($type, $author, $date);
}
/**
* Set enum value.
*
* @param null|string $value
* @param string[] $enum
* @param null|string $default
*
* @return null|string
*
* @todo Merge with the same method in AbstractStyle
*/
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}");
} elseif ($value === null || trim($value) == '') {
$value = $default;
}
return $value;
}
}
View on GitHub (pinned to aef95c0415)