symfony/http-foundation · error · InvalidArgumentException

Expected a scalar, or an array as a 2nd argument to

Error message

Expected a scalar, or an array as a 2nd argument to "%s()", "%s" given.

What it means

InputBag::set() only accepts scalar, array, null, or Stringable values. When a value of any other type (e.g. an object, resource, or closure) is passed as the second argument, the method throws this InvalidArgumentException instead of silently storing a value that would later fail string coercion. This guards the bag's contract that parameters can be safely consumed as request input.

Solutions

  1. Cast the value to a scalar before setting it, e.g. (string) $value or $value->format(...) for dates.
  2. If the object is string-convertible, make it implement Stringable (add a __toString() method).
  3. Wrap the object's relevant data in an array if the consumer expects structure.
  4. Use ParameterBag instead of InputBag if you genuinely need to store arbitrary types.
  5. Add an is_scalar()/instanceof Stringable check at the call site to fail early with a clear message.

Example fix

// before
$inputBag->set('createdAt', $createdAt); // DateTimeImmutable: throws

// after
$inputBag->set('createdAt', $createdAt->format(DATE_ATOM));
Defensive patterns

Strategy: type-guard

Validate before calling

// before $bag->set($key, $value)
if (null !== $value && !is_scalar($value) && !is_array($value) && !$value instanceof \Stringable) {
    throw new \UnexpectedValueException(sprintf('Key "%s" must be scalar/array/Stringable, %s given.', $key, get_debug_type($value)));
}

Type guard

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

Try / catch

try {
    $bag->set($key, $value);
} catch (\InvalidArgumentException $e) {
    $logger->warning('Invalid InputBag value', ['key' => $key, 'type' => get_debug_type($value)]);
    $bag->set($key, (string) $value); // or skip
}

Prevention

When it happens

Trigger: Calling $inputBag->set('key', $someObject) where $someObject is not scalar, not an array, not null, and does not implement Stringable; e.g. passing a DateTime, stdClass, or a typed value object without __toString().

Common situations: Developers copying values from a ParameterBag or a service container into an InputBag; passing a request-mapped DTO object instead of its scalar representation; refactoring code that previously used ParameterBag::set() (which accepts anything) onto InputBag, which is stricter.

Understand the failure class

Background: "Must be a positive integer", "Invalid value", "Unsupported": the invalid-argument-value error family, when a library rejects the value you pass — this error's family across 35 libraries.

Related errors


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

Appendix: source

Thrown at InputBag.php:79

    /**
     * Adds input values.
     */
    public function add(array $inputs = []): void
    {
        foreach ($inputs as $input => $value) {
            $this->set($input, $value);
        }
    }

    /**
     * Sets an input by name.
     *
     * @param string|int|float|bool|array|null $value
     */
    public function set(string $key, mixed $value): void
    {
        if (null !== $value && !\is_scalar($value) && !\is_array($value) && !$value instanceof \Stringable) {
            throw new \InvalidArgumentException(\sprintf('Expected a scalar, or an array as a 2nd argument to "%s()", "%s" given.', __METHOD__, get_debug_type($value)));
        }

        $this->parameters[$key] = $value;
    }

    /**
     * Returns the input value converted to integer.
     *
     * @throws BadRequestException if the value cannot be converted to integer
     */
    public function getInt(string $key, int $default = 0): int
    {
        return $this->filter($key, $default, \FILTER_VALIDATE_INT, ['flags' => \FILTER_REQUIRE_SCALAR | \FILTER_NULL_ON_FAILURE]) ?? throw new BadRequestException(\sprintf('Input value "%s" cannot be converted to "int".', $key));
    }

    /**
     * Returns the input value converted to boolean.
     *

View on GitHub (pinned to 5aea19cd67)