twigphp/Twig · error · Twig\Error\RuntimeError

The number formatter padding position

Error message

The number formatter padding position "%s" does not exist, known positions are: "%s".

What it means

When the attribute 'padding_position' is set in $attrs, its value is validated against IntlExtension::NUMBER_PADDING_ATTRIBUTES. An unknown padding position name throws this RuntimeError listing the valid positions.

Solutions

  1. Use one of the positions listed in the error (keys of NUMBER_PADDING_ATTRIBUTES, e.g. 'before_prefix', 'after_prefix', 'before_suffix', 'after_suffix')
  2. Fix the padding position string in the attrs array
  3. Validate configured padding positions against NUMBER_PADDING_ATTRIBUTES keys
  4. Remove padding_position if padding is not required

Example fix

// before
{{ n|format_number(attrs={'padding_position': 'before'}) }}
// after
{{ n|format_number(attrs={'padding_position': 'before_prefix'}) }}
Defensive patterns

Strategy: validation

Validate before calling

if (isset($attrs['padding_position']) && !isset(IntlExtension::NUMBER_PADDING_ATTRIBUTES[$attrs['padding_position']])) { throw new \InvalidArgumentException('Unknown padding position'); }

Type guard

function isKnownPaddingPosition(string $p): bool { return isset(IntlExtension::NUMBER_PADDING_ATTRIBUTES[$p]); }

Try / catch

try { $out = $intl->formatNumber($n, $style, $attrs); } catch (\Twig\Error\RuntimeError $e) { if (str_contains($e->getMessage(), 'padding position')) { unset($attrs['padding_position']); $out = $intl->formatNumber($n, $style, $attrs); } else { throw $e; } }

Prevention

When it happens

Trigger: Calling format_number/format_currency with attrs={'padding_position': X} where X is not a key of NUMBER_PADDING_ATTRIBUTES — e.g. 'left'/'right' variants not in the accepted map, or a raw constant name like 'PAD_BEFORE_PREFIX'.

Common situations: Translating \NumberFormatter::PAD_* constants directly to strings; typos in template arguments; config-driven padding settings not checked.

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 twigphp/Twig@a414c3a491 (2026-09-13). Data as JSON: /api/errors/36fd0842fd8555fb. Report an issue: GitHub.

Appendix: source

Thrown at extra/intl-extra/IntlExtension.php:589

                array_shift($this->numberFormatters);
            }
            $this->numberFormatters[$hash] = new \NumberFormatter($locale, self::NUMBER_STYLES[$style]);
        }

        foreach ($attrs as $name => $value) {
            if (!isset(self::NUMBER_ATTRIBUTES[$name])) {
                throw new RuntimeError(\sprintf('The number formatter attribute "%s" does not exist, known attributes are: "%s".', $name, implode('", "', array_keys(self::NUMBER_ATTRIBUTES))));
            }

            if ('rounding_mode' === $name) {
                if (!isset(self::NUMBER_ROUNDING_ATTRIBUTES[$value])) {
                    throw new RuntimeError(\sprintf('The number formatter rounding mode "%s" does not exist, known modes are: "%s".', $value, implode('", "', array_keys(self::NUMBER_ROUNDING_ATTRIBUTES))));
                }

                $value = self::NUMBER_ROUNDING_ATTRIBUTES[$value];
            } elseif ('padding_position' === $name) {
                if (!isset(self::NUMBER_PADDING_ATTRIBUTES[$value])) {
                    throw new RuntimeError(\sprintf('The number formatter padding position "%s" does not exist, known positions are: "%s".', $value, implode('", "', array_keys(self::NUMBER_PADDING_ATTRIBUTES))));
                }

                $value = self::NUMBER_PADDING_ATTRIBUTES[$value];
            }

            $this->numberFormatters[$hash]->setAttribute(self::NUMBER_ATTRIBUTES[$name], $value);
        }

        foreach ($textAttrs as $name => $value) {
            $this->numberFormatters[$hash]->setTextAttribute(self::NUMBER_TEXT_ATTRIBUTES[$name], $value);
        }

        foreach ($symbols as $name => $value) {
            $this->numberFormatters[$hash]->setSymbol(self::NUMBER_SYMBOLS[$name], $value);
        }

        return $this->numberFormatters[$hash];
    }

View on GitHub (pinned to a414c3a491)