PHPOffice/PHPWord · error · InvalidArgumentException
Invalid type '$type'
Error message
Invalid type '$type'
What it means
Field::setType() validates the requested Word field type (e.g. PAGE, DATE, NUMPAGES) against the hardcoded $fieldsArray whitelist in Field.php. If the type string is not a key of that array, an InvalidArgumentException is thrown and the field type is left unchanged. The library only supports a fixed set of field codes.
Solutions
- Use one of the supported field codes exactly as defined in Field::$fieldsArray (e.g. 'PAGE', 'DATE', 'NUMPAGES', 'TIME', 'FILESIZE').
- Check src/PhpWord/Element/Field.php for the $fieldsArray keys to see all valid types.
- Wrap setType/new Field in a try-catch for InvalidArgumentException if the type comes from user input.
- If a needed field is missing, add it to $fieldsArray in a subclass or upstream PR.
Example fix
// before
$field = $section->addField('PageNum');
// after
$field = $section->addField('PAGE'); Defensive patterns
Strategy: validation
Validate before calling
$valid = ['PAGE','DATE','NUMPAGES','TIME','FILESIZE']; // mirror Field::$fieldsArray keys
if (!in_array($type, array_keys(Field::class ? [] : []), true) && !isset($validMap[$type])) { /* reject */ } Type guard
function isValidFieldType(?string $type): bool { $r = new \ReflectionClass(\PhpOffice\PhpWord\Element\Field::class); $p = $r->getProperty('fieldsArray'); $p->setAccessible(true); return $type !== null && array_key_exists($type, $p->getValue()); } Try / catch
try { $field = $section->addField($type); } catch (\InvalidArgumentException $e) { $field = $section->addField('PAGE'); } Prevention
- Always use exact uppercase field codes from Field::$fieldsArray
- Validate user/config-supplied field names against the whitelist before calling
- Keep a constant list of supported fields in your app code
When it happens
Trigger: Calling new Field($section, 'NOTAFIELD') or $field->setType('Foo') with any string not present as a top-level key of Field::$fieldsArray (case-sensitive).
Common situations: Typo'd field codes (e.g. 'Page' instead of 'PAGE'), attempting a Word field code the library hasn't whitelisted, or dynamically building field names from user/config input.
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 property '$propkey'
- Invalid option '$optionkey', possible values are ' …
- 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/8f227471bad21110.
Report an issue: GitHub.
Appendix: source
Thrown at src/PhpWord/Element/Field.php:200
$this->setOptions($options);
$this->setText($text);
$this->setFontStyle($fontStyle);
}
/**
* Set Field type.
*
* @param string $type
*
* @return string
*/
public function setType($type = null)
{
if (isset($type)) {
if (isset($this->fieldsArray[$type])) {
$this->type = $type;
} else {
throw new InvalidArgumentException("Invalid type '$type'");
}
}
return $this->type;
}
/**
* Get Field type.
*
* @return string
*/
public function getType()
{
return $this->type;
}
/**
* Set Field properties.View on GitHub (pinned to aef95c0415)