diff --git a/src/Symfony/Component/Form/Extension/HttpFoundation/HttpFoundationRequestHandler.php b/src/Symfony/Component/Form/Extension/HttpFoundation/HttpFoundationRequestHandler.php index 47ce62feefe14..a37354efe3fdc 100644 --- a/src/Symfony/Component/Form/Extension/HttpFoundation/HttpFoundationRequestHandler.php +++ b/src/Symfony/Component/Form/Extension/HttpFoundation/HttpFoundationRequestHandler.php @@ -63,7 +63,7 @@ public function handleRequest(FormInterface $form, $request = null) return; } - $data = $request->query->all()[$name]; + $data = $request->query->getAny($name); } } else { // Mark the form with an error if the uploaded size was too large @@ -87,7 +87,7 @@ public function handleRequest(FormInterface $form, $request = null) $files = $request->files->all(); } elseif ($request->request->has($name) || $request->files->has($name)) { $default = $form->getConfig()->getCompound() ? [] : null; - $params = $request->request->all()[$name] ?? $default; + $params = $request->request->getAny($name, $default); $files = $request->files->get($name, $default); } else { // Don't submit the form if it is not present in the request diff --git a/src/Symfony/Component/HttpFoundation/InputBag.php b/src/Symfony/Component/HttpFoundation/InputBag.php index b0a9ef35982e7..487e5e9780d7b 100644 --- a/src/Symfony/Component/HttpFoundation/InputBag.php +++ b/src/Symfony/Component/HttpFoundation/InputBag.php @@ -42,6 +42,18 @@ public function get(string $key, $default = null) return $this === $value ? $default : $value; } + /** + * Returns any input value by name. + * + * @param mixed $default The default value if the input key does not exist + * + * @return mixed + */ + public function getAny(string $key, $default = null) + { + return \array_key_exists($key, $this->parameters) ? $this->parameters[$key] : $default; + } + /** * Returns the inputs. * @@ -99,7 +111,7 @@ public function set(string $key, $value) */ public function filter(string $key, $default = null, int $filter = FILTER_DEFAULT, $options = []) { - $value = $this->has($key) ? $this->all()[$key] : $default; + $value = $this->getAny($key, $default); // Always turn $options into an array - this allows filter_var option shortcuts. if (!\is_array($options) && $options) { diff --git a/src/Symfony/Component/HttpFoundation/Tests/InputBagTest.php b/src/Symfony/Component/HttpFoundation/Tests/InputBagTest.php index febe5eda62f01..8c9a596270cd8 100644 --- a/src/Symfony/Component/HttpFoundation/Tests/InputBagTest.php +++ b/src/Symfony/Component/HttpFoundation/Tests/InputBagTest.php @@ -36,6 +36,17 @@ public function testGetDoesNotUseDeepByDefault() $this->assertNull($bag->get('foo[bar]')); } + public function testGetAny() + { + $bag = new InputBag(['foo' => 'bar', 'null' => null, 'arr' => [1], 'num' => 1]); + + $this->assertSame('bar', $bag->getAny('foo'), '->getAny() gets the value of a parameter (string)'); + $this->assertSame([1], $bag->getAny('arr'), '->getAny() gets the value of a parameter (array)'); + $this->assertSame(1, $bag->getAny('num'), '->getAny() gets the value of a parameter (int)'); + $this->assertSame([], $bag->getAny('unknown', []), '->getAny() returns second argument as default if a parameter is not defined'); + $this->assertNull($bag->getAny('null'), '->getAny() returns null if null is set'); + } + public function testAllWithInputKey() { $bag = new InputBag(['foo' => ['bar', 'baz'], 'null' => null]);
Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.
Alternative Proxies: