symfony/http-foundation · error · UnexpectedValueException

Parameter value " " cannot be filtered.

Error message

Parameter value "%s" cannot be filtered.

What it means

ParameterBag::filter() applies a PHP filter (filter_var) to a stored parameter. It throws UnexpectedValueException when the stored value is an object that does not implement Stringable, because filter_var cannot meaningfully filter such values. Arrays are auto-handled by adding FILTER_REQUIRE_ARRAY, but plain objects are rejected.

Solutions

  1. Store scalars (string/int/bool) or Stringable objects in the bag instead of raw objects
  2. Cast or extract a scalar from the object before filtering (e.g. ->getTimestamp(), (string) $obj with __toString)
  3. Retrieve with get() and filter manually after converting to a scalar
  4. Fix the code path that sets the object into the parameter bag

Example fix

// before
$ts = $bag->getInt('created_at'); // 'created_at' holds a DateTime -> throws
// after
$ts = $bag->get('created_at') instanceof \DateTime ? $bag->get('created_at')->getTimestamp() : $bag->getInt('created_at');
Defensive patterns

Strategy: type-guard

Validate before calling

if (isset($value) && is_object($value) && !$value instanceof \Stringable) {
    throw new \InvalidArgumentException('Parameter must be scalar or Stringable before filtering');
}

Type guard

function isFilterable(mixed $v): bool {
    return null === $v || is_scalar($v) || $v instanceof \Stringable || is_array($v);
}

Try / catch

try {
    $n = $bag->filter('key', 0, \FILTER_VALIDATE_INT);
} catch (\UnexpectedValueException $e) {
    $n = 0;
}

Prevention

When it happens

Trigger: $bag->filter('key', default, FILTER_VALIDATE_INT) (or any filter) where the parameter holds a non-Stringable object such as a DateTime, stdClass, or a service instance. Also indirectly via getInt/getBoolean/filterCallback when the stored value is such an object.

Common situations: Someone put an object into the bag (e.g. $bag->set('limit', new \DateTime())) while other code assumes scalars; dependency injection misconfiguration; deserialization produced objects instead of scalars.

Understand the failure class

Background: Type mismatch errors: IllegalArgumentException, TypeError and type guards across 150 open-source libraries — this error's family across 150 libraries.

Related errors


AI-assisted analysis of symfony/http-foundation@5aea19cd67 (2026-09-13). Data as JSON: /api/errors/3a18138f2a5c3797. Report an issue: GitHub.

Appendix: source

Thrown at ParameterBag.php:232

     * @throws UnexpectedValueException if the parameter value is a non-stringable object
     * @throws UnexpectedValueException if the parameter value is invalid and \FILTER_NULL_ON_FAILURE is not set
     */
    public function filter(string $key, mixed $default = null, int $filter = \FILTER_DEFAULT, mixed $options = []): mixed
    {
        $value = $this->get($key, $default);

        // Always turn $options into an array - this allows filter_var option shortcuts.
        if (!\is_array($options) && $options) {
            $options = ['flags' => $options];
        }

        // Add a convenience check for arrays.
        if (\is_array($value) && !isset($options['flags'])) {
            $options['flags'] = \FILTER_REQUIRE_ARRAY;
        }

        if (\is_object($value) && !$value instanceof \Stringable) {
            throw new UnexpectedValueException(\sprintf('Parameter value "%s" cannot be filtered.', $key));
        }

        if ((\FILTER_CALLBACK & $filter) && !(($options['options'] ?? null) instanceof \Closure)) {
            throw new \InvalidArgumentException(\sprintf('A Closure must be passed to "%s()" when FILTER_CALLBACK is used, "%s" given.', __METHOD__, get_debug_type($options['options'] ?? null)));
        }

        $options['flags'] ??= 0;
        $nullOnFailure = $options['flags'] & \FILTER_NULL_ON_FAILURE;
        $options['flags'] |= \FILTER_NULL_ON_FAILURE;

        $value = filter_var($value, $filter, $options);

        if (null !== $value || $nullOnFailure) {
            return $value;
        }

        throw new \UnexpectedValueException(\sprintf('Parameter value "%s" is invalid and flag "FILTER_NULL_ON_FAILURE" was not set.', $key));
    }

View on GitHub (pinned to 5aea19cd67)