PHPOffice/PHPWord · error · InvalidArgumentException

$value is not a valid value for $calledClass, possible…

Error message

$value is not a valid value for $calledClass, possible values are ' . implode(', ', $values)

What it means

AbstractEnum::validate() is the guard used by PhpWord value objects (alignment, vertical alignment, line rule, unit). When a setter receives a value not among the class constants, it throws InvalidArgumentException listing the class name and every accepted constant value. It exists to fail fast on invalid enum-style inputs rather than silently producing malformed document XML.

Solutions

  1. Look at the exception message: it lists all valid values for the called class; use one of them exactly (case-sensitive).
  2. Use the class constants (e.g. Alignment::CENTER, LineRule::AUTO) instead of raw strings.
  3. Normalize/whitelist user or config input against the enum's isValid() before calling the setter.
  4. If a value seems missing, check whether the constant was renamed in your PhpWord version.

Example fix

// before
$cell->setVAlign('middle');
// after
use PhpOffice\PhpWord\SimpleType\VerticalJc;
$cell->setVAlign(VerticalJc::CENTER); // 'center'
Defensive patterns

Strategy: validation

Validate before calling

use PhpOffice\PhpWord\SimpleType\VerticalJc;
if (!in_array($value, VerticalJc::getConstants(), true)) {
    throw new InvalidArgumentException("Invalid vAlign: $value");
}
$cell->setVAlign($value);

Try / catch

try {
    $cell->setVAlign($value);
} catch (\InvalidArgumentException $e) {
    $logger->warning('Invalid alignment value', ['value' => $value, 'msg' => $e->getMessage()]);
    $cell->setVAlign(VerticalJc::CENTER); // safe default
}

Prevention

When it happens

Trigger: Calling setTextAlignment()/setVAlign()/setLineRule()/setUnit() (or anything that funnels into AbstractEnum::validate) with a string that is not one of the class constants, e.g. setVAlign('centered') instead of 'center', or setLineRule('exact') instead of LineRule::EXACT. Case-sensitivity matters: 'Center' or 'CENTER' will not match.

Common situations: Hand-written style configuration copied from Word UI wording instead of the library constants; typo'd constant names; values read from user input or config files without whitelisting; upgrading where a constant was renamed.

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


AI-assisted analysis of PHPOffice/PHPWord@aef95c0415 (2026-09-14). Data as JSON: /api/errors/4c83531a7dda6a46. Report an issue: GitHub.

Appendix: source

Thrown at src/PhpWord/Shared/AbstractEnum.php:77

    public static function isValid($value)
    {
        $values = array_values(self::getConstants());

        return in_array($value, $values, true);
    }

    /**
     * Validates that the value passed is a valid value.
     *
     * @param string $value
     */
    public static function validate($value): void
    {
        if (!self::isValid($value)) {
            $calledClass = static::class;
            $values = array_values(self::getConstants());

            throw new InvalidArgumentException("$value is not a valid value for $calledClass, possible values are " . implode(', ', $values));
        }
    }
}

View on GitHub (pinned to aef95c0415)