From d47a57f6acaa450e782d48fa67ebda54db6ee602 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E2=80=9CFilip?= Date: Wed, 19 Jan 2022 16:18:06 +0100 Subject: [PATCH 01/13] [Form] Missing Data Handling (checkbox) --- .../HttpFoundationRequestHandler.php | 22 +++-- .../Component/Form/MissingDataHandler.php | 68 ++++++++++++++ .../Component/Form/NativeRequestHandler.php | 22 +++-- .../Form/Tests/AbstractRequestHandlerTest.php | 90 +++++++++++++++++++ 4 files changed, 192 insertions(+), 10 deletions(-) create mode 100644 src/Symfony/Component/Form/MissingDataHandler.php diff --git a/src/Symfony/Component/Form/Extension/HttpFoundation/HttpFoundationRequestHandler.php b/src/Symfony/Component/Form/Extension/HttpFoundation/HttpFoundationRequestHandler.php index 3b1aaebf02648..3496ccbd5aeb4 100644 --- a/src/Symfony/Component/Form/Extension/HttpFoundation/HttpFoundationRequestHandler.php +++ b/src/Symfony/Component/Form/Extension/HttpFoundation/HttpFoundationRequestHandler.php @@ -14,6 +14,7 @@ use Symfony\Component\Form\Exception\UnexpectedTypeException; use Symfony\Component\Form\FormError; use Symfony\Component\Form\FormInterface; +use Symfony\Component\Form\MissingDataHandler; use Symfony\Component\Form\RequestHandlerInterface; use Symfony\Component\Form\Util\ServerParams; use Symfony\Component\HttpFoundation\File\File; @@ -29,10 +30,12 @@ class HttpFoundationRequestHandler implements RequestHandlerInterface { private ServerParams $serverParams; + private MissingDataHandler $missingDataHandler; public function __construct(ServerParams $serverParams = null) { $this->serverParams = $serverParams ?? new ServerParams(); + $this->missingDataHandler = new MissingDataHandler(); } public function handleRequest(FormInterface $form, mixed $request = null) @@ -54,13 +57,13 @@ public function handleRequest(FormInterface $form, mixed $request = null) if ('' === $name) { $data = $request->query->all(); } else { - // Don't submit GET requests if the form's name does not exist - // in the request - if (!$request->query->has($name)) { + $missingData = $this->missingDataHandler->missingData; + + if ($missingData === $data = $request->query->get($name) ?? $missingData) { + // Don't submit GET requests if the form's name does not exist + // in the request return; } - - $data = $request->query->all()[$name]; } } else { // Mark the form with an error if the uploaded size was too large @@ -87,6 +90,15 @@ public function handleRequest(FormInterface $form, mixed $request = null) $params = $request->request->all()[$name] ?? $default; $files = $request->files->get($name, $default); } else { + $params = $this->missingDataHandler->missingData; + $files = null; + } + + if ('PATCH' !== $method) { + $params = $this->missingDataHandler->handle($form, $params); + } + + if ($this->missingDataHandler->missingData === $params) { // Don't submit the form if it is not present in the request return; } diff --git a/src/Symfony/Component/Form/MissingDataHandler.php b/src/Symfony/Component/Form/MissingDataHandler.php new file mode 100644 index 0000000000000..b5b26a7565ff6 --- /dev/null +++ b/src/Symfony/Component/Form/MissingDataHandler.php @@ -0,0 +1,68 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Symfony\Component\Form; + +use Symfony\Component\Form\Extension\Core\Type\CheckboxType; +use Symfony\Component\Form\FormInterface; +use Symfony\Component\Form\ResolvedFormTypeInterface; + +class MissingDataHandler +{ + public readonly \stdClass $missingData; + + public function __construct() + { + $this->missingData = new \stdClass(); + } + + public function handle(FormInterface $form, mixed $data): mixed + { + $processedData = $this->handleMissingData($form, $data); + + return $processedData === $this->missingData ? $data : $processedData; + } + + private function handleMissingData(FormInterface $form, mixed $data): mixed + { + if ($form->getConfig()->getType() instanceof ResolvedFormTypeInterface && $form->getConfig()->getType()->getInnerType() instanceof CheckboxType) { + $falseValues = $form->getConfig()->getOption('false_values'); + + if ($data === $this->missingData) { + return $falseValues[0]; + } + + if (\in_array($data, $falseValues)) { + return $data; + } + } + + if (null === $data || $this->missingData === $data) { + $data = $form->getConfig()->getCompound() ? [] : $data; + } + + if (\is_array($data)) { + $children = $form->getConfig()->getCompound() ? $form->all() : [$form]; + + foreach ($children as $child) { + $value = $this->handleMissingData($child, \array_key_exists($child->getName(), $data) ? $data[$child->getName()] : $this->missingData); + + if ($this->missingData !== $value) { + $data[$child->getName()] = $value; + } + } + + return $data ?: $this->missingData; + } + + return $data; + } +} diff --git a/src/Symfony/Component/Form/NativeRequestHandler.php b/src/Symfony/Component/Form/NativeRequestHandler.php index cf193398c8318..fc314b2cd8c84 100644 --- a/src/Symfony/Component/Form/NativeRequestHandler.php +++ b/src/Symfony/Component/Form/NativeRequestHandler.php @@ -12,6 +12,7 @@ namespace Symfony\Component\Form; use Symfony\Component\Form\Exception\UnexpectedTypeException; +use Symfony\Component\Form\MissingDataHandler; use Symfony\Component\Form\Util\ServerParams; /** @@ -22,6 +23,7 @@ class NativeRequestHandler implements RequestHandlerInterface { private ServerParams $serverParams; + private MissingDataHandler $missingDataHandler; /** * The allowed keys of the $_FILES array. @@ -37,6 +39,7 @@ class NativeRequestHandler implements RequestHandlerInterface public function __construct(ServerParams $params = null) { $this->serverParams = $params ?? new ServerParams(); + $this->missingDataHandler = new MissingDataHandler(); } /** @@ -61,13 +64,13 @@ public function handleRequest(FormInterface $form, mixed $request = null) if ('' === $name) { $data = $_GET; } else { - // Don't submit GET requests if the form's name does not exist - // in the request - if (!isset($_GET[$name])) { + $missingData = $this->missingDataHandler->missingData; + + if ($missingData === $data = $this->missingDataHandler->handle($form, $_GET[$name] ?? $missingData)) { + // Don't submit GET requests if the form's name does not exist + // in the request return; } - - $data = $_GET[$name]; } } else { // Mark the form with an error if the uploaded size was too large @@ -99,6 +102,15 @@ public function handleRequest(FormInterface $form, mixed $request = null) $params = \array_key_exists($name, $_POST) ? $_POST[$name] : $default; $files = \array_key_exists($name, $fixedFiles) ? $fixedFiles[$name] : $default; } else { + $params = $this->missingDataHandler->missingData; + $files = null; + } + + if ('PATCH' !== $method) { + $params = $this->missingDataHandler->handle($form, $params); + } + + if ($this->missingDataHandler->missingData === $params) { // Don't submit the form if it is not present in the request return; } diff --git a/src/Symfony/Component/Form/Tests/AbstractRequestHandlerTest.php b/src/Symfony/Component/Form/Tests/AbstractRequestHandlerTest.php index 61b8dc379148a..7011aa29c4591 100644 --- a/src/Symfony/Component/Form/Tests/AbstractRequestHandlerTest.php +++ b/src/Symfony/Component/Form/Tests/AbstractRequestHandlerTest.php @@ -14,6 +14,9 @@ use PHPUnit\Framework\TestCase; use Symfony\Component\EventDispatcher\EventDispatcher; use Symfony\Component\Form\Extension\Core\DataMapper\DataMapper; +use Symfony\Component\Form\Extension\Core\Type\CheckboxType; +use Symfony\Component\Form\Extension\Core\Type\CollectionType; +use Symfony\Component\Form\Extension\Core\Type\FormType; use Symfony\Component\Form\Form; use Symfony\Component\Form\FormBuilder; use Symfony\Component\Form\FormError; @@ -65,6 +68,93 @@ public function getNormalizedIniPostMaxSize(): string $this->request = null; } + /** + * @dataProvider methodExceptPatchProvider + */ + public function testSubmitCheckboxInCollectionFormWithEmptyData($method) + { + $form = $this->factory->create(CollectionType::class, [true, false, true], [ + 'entry_type' => CheckboxType::class, + 'method' => $method, + ]); + + $this->setRequestData($method, [ + ]); + + $this->requestHandler->handleRequest($form, $this->request); + + $this->assertEqualsCanonicalizing([false, false, false], $form->getData()); + } + + /** + * @dataProvider methodExceptPatchProvider + */ + public function testSubmitCheckboxInCollectionFormWithPartialData($method) + { + $form = $this->factory->create(CollectionType::class, [true, false, true], [ + 'entry_type' => CheckboxType::class, + 'method' => $method, + ]); + + $this->setRequestData($method, [ + 'collection' => [ + 1 => true, + ], + ]); + + $this->requestHandler->handleRequest($form, $this->request); + + $this->assertEqualsCanonicalizing([false, true, false], $form->getData()); + } + + /** + * @dataProvider methodExceptPatchProvider + */ + public function testSubmitCheckboxFormWithEmptyData($method) + { + $form = $this->factory->create(FormType::class, ['subform' => ['checkbox' => true]], [ + 'method' => $method, + ]) + ->add('subform', FormType::class, [ + 'compound' => true, + ]); + + $form->get('subform') + ->add('checkbox', CheckboxType::class); + + $this->setRequestData($method, []); + + $this->requestHandler->handleRequest($form, $this->request); + + $this->assertEquals(['subform' => ['checkbox' => false]], $form->getData()); + } + + /** + * @dataProvider methodExceptPatchProvider + */ + public function testSubmitSimpleCheckboxFormWithEmptyData($method) + { + $form = $this->factory->createNamed('checkbox', CheckboxType::class, true, [ + 'method' => $method, + ]); + + $this->setRequestData($method, []); + + $this->requestHandler->handleRequest($form, $this->request); + + $this->assertFalse($form->getData()); + } + + public function methodExceptPatchProvider() + { + return [ + ['POST'], + ['PUT'], + ['DELETE'], + ['GET'], + ]; + } + public function methodExceptGetProvider() { return [ From 753382d34dfdc16c06a4e82a60d7dc3a6ce20389 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E2=80=9CFilip?= Date: Tue, 2 Aug 2022 17:17:59 +0200 Subject: [PATCH 02/13] Fix failing tests (missingDataHandler was not used in HttpFoundationRequestHandler when handling GET requests) --- .../Extension/HttpFoundation/HttpFoundationRequestHandler.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Symfony/Component/Form/Extension/HttpFoundation/HttpFoundationRequestHandler.php b/src/Symfony/Component/Form/Extension/HttpFoundation/HttpFoundationRequestHandler.php index 3496ccbd5aeb4..3df3911fe7f7d 100644 --- a/src/Symfony/Component/Form/Extension/HttpFoundation/HttpFoundationRequestHandler.php +++ b/src/Symfony/Component/Form/Extension/HttpFoundation/HttpFoundationRequestHandler.php @@ -59,7 +59,7 @@ public function handleRequest(FormInterface $form, mixed $request = null) } else { $missingData = $this->missingDataHandler->missingData; - if ($missingData === $data = $request->query->get($name) ?? $missingData) { + if ($missingData === $data = $this->missingDataHandler->handle($form, $request->query->get($name) ?? $missingData)) { // Don't submit GET requests if the form's name does not exist // in the request return; From 9da6d1c1d77ff315c7e50b024def8e376236e472 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E2=80=9CFilip?= Date: Tue, 2 Aug 2022 17:23:44 +0200 Subject: [PATCH 03/13] Remove unnecessary 'use' --- src/Symfony/Component/Form/MissingDataHandler.php | 2 -- src/Symfony/Component/Form/NativeRequestHandler.php | 1 - 2 files changed, 3 deletions(-) diff --git a/src/Symfony/Component/Form/MissingDataHandler.php b/src/Symfony/Component/Form/MissingDataHandler.php index b5b26a7565ff6..a1a61258a4a46 100644 --- a/src/Symfony/Component/Form/MissingDataHandler.php +++ b/src/Symfony/Component/Form/MissingDataHandler.php @@ -12,8 +12,6 @@ namespace Symfony\Component\Form; use Symfony\Component\Form\Extension\Core\Type\CheckboxType; -use Symfony\Component\Form\FormInterface; -use Symfony\Component\Form\ResolvedFormTypeInterface; class MissingDataHandler { diff --git a/src/Symfony/Component/Form/NativeRequestHandler.php b/src/Symfony/Component/Form/NativeRequestHandler.php index fc314b2cd8c84..e3632713623f9 100644 --- a/src/Symfony/Component/Form/NativeRequestHandler.php +++ b/src/Symfony/Component/Form/NativeRequestHandler.php @@ -12,7 +12,6 @@ namespace Symfony\Component\Form; use Symfony\Component\Form\Exception\UnexpectedTypeException; -use Symfony\Component\Form\MissingDataHandler; use Symfony\Component\Form\Util\ServerParams; /** From 82535abbe5f7fef2e51b797e962f41a499718f35 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E2=80=9CFilip?= Date: Tue, 2 Aug 2022 17:51:35 +0200 Subject: [PATCH 04/13] Force push --- src/Symfony/Component/Form/MissingDataHandler.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Symfony/Component/Form/MissingDataHandler.php b/src/Symfony/Component/Form/MissingDataHandler.php index a1a61258a4a46..d62db0d08f287 100644 --- a/src/Symfony/Component/Form/MissingDataHandler.php +++ b/src/Symfony/Component/Form/MissingDataHandler.php @@ -35,7 +35,7 @@ private function handleMissingData(FormInterface $form, mixed $data): mixed $falseValues = $form->getConfig()->getOption('false_values'); if ($data === $this->missingData) { - return $falseValues[0]; + return $falseValues[0]; } if (\in_array($data, $falseValues)) { From 4eb6dad53ff57210a7e1780314e6efafb27f2748 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E2=80=9CFilip?= Date: Tue, 2 Aug 2022 17:51:51 +0200 Subject: [PATCH 05/13] Force push --- src/Symfony/Component/Form/MissingDataHandler.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Symfony/Component/Form/MissingDataHandler.php b/src/Symfony/Component/Form/MissingDataHandler.php index d62db0d08f287..a1a61258a4a46 100644 --- a/src/Symfony/Component/Form/MissingDataHandler.php +++ b/src/Symfony/Component/Form/MissingDataHandler.php @@ -35,7 +35,7 @@ private function handleMissingData(FormInterface $form, mixed $data): mixed $falseValues = $form->getConfig()->getOption('false_values'); if ($data === $this->missingData) { - return $falseValues[0]; + return $falseValues[0]; } if (\in_array($data, $falseValues)) { From 663984ed061d7d0611f2cea006bc8417ec47b61c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E2=80=9CFilip?= Date: Tue, 2 Aug 2022 18:09:04 +0200 Subject: [PATCH 06/13] Add original logic (has/all) back --- .../HttpFoundation/HttpFoundationRequestHandler.php | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/src/Symfony/Component/Form/Extension/HttpFoundation/HttpFoundationRequestHandler.php b/src/Symfony/Component/Form/Extension/HttpFoundation/HttpFoundationRequestHandler.php index 3df3911fe7f7d..e3834621aabf4 100644 --- a/src/Symfony/Component/Form/Extension/HttpFoundation/HttpFoundationRequestHandler.php +++ b/src/Symfony/Component/Form/Extension/HttpFoundation/HttpFoundationRequestHandler.php @@ -59,7 +59,14 @@ public function handleRequest(FormInterface $form, mixed $request = null) } else { $missingData = $this->missingDataHandler->missingData; - if ($missingData === $data = $this->missingDataHandler->handle($form, $request->query->get($name) ?? $missingData)) { + if ($request->query->has($name)) { + $data = $request->query->all()[$name]; + } + else { + $data = $missingData; + } + + if ($missingData === $data = $this->missingDataHandler->handle($form, $data)) { // Don't submit GET requests if the form's name does not exist // in the request return; From 591b4b979ccf15e5dc84ac334f2d87d34d95c4d8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E2=80=9CFilip?= Date: Tue, 2 Aug 2022 18:11:04 +0200 Subject: [PATCH 07/13] CS --- .../Extension/HttpFoundation/HttpFoundationRequestHandler.php | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/Symfony/Component/Form/Extension/HttpFoundation/HttpFoundationRequestHandler.php b/src/Symfony/Component/Form/Extension/HttpFoundation/HttpFoundationRequestHandler.php index e3834621aabf4..b4f9d9a1d9ead 100644 --- a/src/Symfony/Component/Form/Extension/HttpFoundation/HttpFoundationRequestHandler.php +++ b/src/Symfony/Component/Form/Extension/HttpFoundation/HttpFoundationRequestHandler.php @@ -61,8 +61,7 @@ public function handleRequest(FormInterface $form, mixed $request = null) if ($request->query->has($name)) { $data = $request->query->all()[$name]; - } - else { + } else { $data = $missingData; } From d1853fd938b26093778d0c03f6d49670ce8ad1a4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E2=80=9CFilip?= Date: Tue, 2 Aug 2022 18:13:26 +0200 Subject: [PATCH 08/13] CS --- .../HttpFoundation/HttpFoundationRequestHandler.php | 8 +------- 1 file changed, 1 insertion(+), 7 deletions(-) diff --git a/src/Symfony/Component/Form/Extension/HttpFoundation/HttpFoundationRequestHandler.php b/src/Symfony/Component/Form/Extension/HttpFoundation/HttpFoundationRequestHandler.php index b4f9d9a1d9ead..9c1bfc75ae70a 100644 --- a/src/Symfony/Component/Form/Extension/HttpFoundation/HttpFoundationRequestHandler.php +++ b/src/Symfony/Component/Form/Extension/HttpFoundation/HttpFoundationRequestHandler.php @@ -59,13 +59,7 @@ public function handleRequest(FormInterface $form, mixed $request = null) } else { $missingData = $this->missingDataHandler->missingData; - if ($request->query->has($name)) { - $data = $request->query->all()[$name]; - } else { - $data = $missingData; - } - - if ($missingData === $data = $this->missingDataHandler->handle($form, $data)) { + if ($missingData === $data = $this->missingDataHandler->handle($form, $request->query->all()[$name] ?? $missingData)) { // Don't submit GET requests if the form's name does not exist // in the request return; From 5a5fe0a11bb4b3ec7d60977eaf292bc16dd48b13 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E2=80=9CFilip?= Date: Wed, 3 Aug 2022 00:58:02 +0200 Subject: [PATCH 09/13] Handle uninitialized FormConfigBuilder::$type --- .../Component/Form/MissingDataHandler.php | 21 +++++++++++++------ 1 file changed, 15 insertions(+), 6 deletions(-) diff --git a/src/Symfony/Component/Form/MissingDataHandler.php b/src/Symfony/Component/Form/MissingDataHandler.php index a1a61258a4a46..c13a88840e6b4 100644 --- a/src/Symfony/Component/Form/MissingDataHandler.php +++ b/src/Symfony/Component/Form/MissingDataHandler.php @@ -31,16 +31,25 @@ public function handle(FormInterface $form, mixed $data): mixed private function handleMissingData(FormInterface $form, mixed $data): mixed { - if ($form->getConfig()->getType() instanceof ResolvedFormTypeInterface && $form->getConfig()->getType()->getInnerType() instanceof CheckboxType) { - $falseValues = $form->getConfig()->getOption('false_values'); + try { + if ($form->getConfig()->getType() instanceof ResolvedFormTypeInterface && $form->getConfig()->getType()->getInnerType() instanceof CheckboxType) { + $falseValues = $form->getConfig()->getOption('false_values'); - if ($data === $this->missingData) { - return $falseValues[0]; - } + if ($data === $this->missingData) { + return $falseValues[0]; + } - if (\in_array($data, $falseValues)) { + if (\in_array($data, $falseValues)) { + return $data; + } + } + } + catch (\Error $error) { + if ($error->getMessage() === 'Typed property Symfony\Component\Form\FormConfigBuilder::$type must not be accessed before initialization') { return $data; } + + throw $error; } if (null === $data || $this->missingData === $data) { From 474ac3062336aabe57e3341af30dcde0a094db96 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E2=80=9CFilip?= Date: Wed, 3 Aug 2022 00:59:14 +0200 Subject: [PATCH 10/13] CS --- src/Symfony/Component/Form/MissingDataHandler.php | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/src/Symfony/Component/Form/MissingDataHandler.php b/src/Symfony/Component/Form/MissingDataHandler.php index c13a88840e6b4..7d2b33caa1ca5 100644 --- a/src/Symfony/Component/Form/MissingDataHandler.php +++ b/src/Symfony/Component/Form/MissingDataHandler.php @@ -43,9 +43,8 @@ private function handleMissingData(FormInterface $form, mixed $data): mixed return $data; } } - } - catch (\Error $error) { - if ($error->getMessage() === 'Typed property Symfony\Component\Form\FormConfigBuilder::$type must not be accessed before initialization') { + } catch (\Error $error) { + if ('Typed property Symfony\Component\Form\FormConfigBuilder::$type must not be accessed before initialization' === $error->getMessage()) { return $data; } From ab3c101d467a0849002db065f00d39f57b2a5bd9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E2=80=9CFilip?= Date: Wed, 3 Aug 2022 01:10:51 +0200 Subject: [PATCH 11/13] Force restart tests --- .gitignore | 1 + 1 file changed, 1 insertion(+) diff --git a/.gitignore b/.gitignore index 0c37517192aba..1244bfe47d7b4 100644 --- a/.gitignore +++ b/.gitignore @@ -8,3 +8,4 @@ composer.phar package.tar /packages.json /.phpunit + From 5ef1388f8d8523d713fee28f3b6a88bb391e23f5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E2=80=9CFilip?= Date: Wed, 3 Aug 2022 10:05:06 +0200 Subject: [PATCH 12/13] [WIP] Rebase to 6.2 --- .gitignore | 1 - src/Symfony/Component/Form/ButtonBuilder.php | 4 ++-- .../Component/Form/FormConfigBuilder.php | 4 ++-- .../Component/Form/FormConfigInterface.php | 2 +- .../Component/Form/MissingDataHandler.php | 20 ++++++------------- 5 files changed, 11 insertions(+), 20 deletions(-) diff --git a/.gitignore b/.gitignore index 1244bfe47d7b4..0c37517192aba 100644 --- a/.gitignore +++ b/.gitignore @@ -8,4 +8,3 @@ composer.phar package.tar /packages.json /.phpunit - diff --git a/src/Symfony/Component/Form/ButtonBuilder.php b/src/Symfony/Component/Form/ButtonBuilder.php index bfba1d6cd0970..a4e92f50d6927 100644 --- a/src/Symfony/Component/Form/ButtonBuilder.php +++ b/src/Symfony/Component/Form/ButtonBuilder.php @@ -29,7 +29,7 @@ class ButtonBuilder implements \IteratorAggregate, FormBuilderInterface protected $locked = false; private bool $disabled = false; - private ResolvedFormTypeInterface $type; + private ?ResolvedFormTypeInterface $type = null; private string $name; private array $attributes = []; private array $options; @@ -453,7 +453,7 @@ public function getCompound(): bool /** * Returns the form type used to construct the button. */ - public function getType(): ResolvedFormTypeInterface + public function getType(): ?ResolvedFormTypeInterface { return $this->type; } diff --git a/src/Symfony/Component/Form/FormConfigBuilder.php b/src/Symfony/Component/Form/FormConfigBuilder.php index e18db7ed8e320..7baab9a1ed5c7 100644 --- a/src/Symfony/Component/Form/FormConfigBuilder.php +++ b/src/Symfony/Component/Form/FormConfigBuilder.php @@ -41,7 +41,7 @@ class FormConfigBuilder implements FormConfigBuilderInterface private bool $byReference = true; private bool $inheritData = false; private bool $compound = false; - private ResolvedFormTypeInterface $type; + private ?ResolvedFormTypeInterface $type = null; private array $viewTransformers = []; private array $modelTransformers = []; private ?DataMapperInterface $dataMapper = null; @@ -197,7 +197,7 @@ public function getCompound(): bool return $this->compound; } - public function getType(): ResolvedFormTypeInterface + public function getType(): ?ResolvedFormTypeInterface { return $this->type; } diff --git a/src/Symfony/Component/Form/FormConfigInterface.php b/src/Symfony/Component/Form/FormConfigInterface.php index 93d1998ec2620..defae6bf6ac3b 100644 --- a/src/Symfony/Component/Form/FormConfigInterface.php +++ b/src/Symfony/Component/Form/FormConfigInterface.php @@ -66,7 +66,7 @@ public function getCompound(): bool; /** * Returns the resolved form type used to construct the form. */ - public function getType(): ResolvedFormTypeInterface; + public function getType(): ?ResolvedFormTypeInterface; /** * Returns the view transformers of the form. diff --git a/src/Symfony/Component/Form/MissingDataHandler.php b/src/Symfony/Component/Form/MissingDataHandler.php index 7d2b33caa1ca5..a1a61258a4a46 100644 --- a/src/Symfony/Component/Form/MissingDataHandler.php +++ b/src/Symfony/Component/Form/MissingDataHandler.php @@ -31,24 +31,16 @@ public function handle(FormInterface $form, mixed $data): mixed private function handleMissingData(FormInterface $form, mixed $data): mixed { - try { - if ($form->getConfig()->getType() instanceof ResolvedFormTypeInterface && $form->getConfig()->getType()->getInnerType() instanceof CheckboxType) { - $falseValues = $form->getConfig()->getOption('false_values'); + if ($form->getConfig()->getType() instanceof ResolvedFormTypeInterface && $form->getConfig()->getType()->getInnerType() instanceof CheckboxType) { + $falseValues = $form->getConfig()->getOption('false_values'); - if ($data === $this->missingData) { - return $falseValues[0]; - } - - if (\in_array($data, $falseValues)) { - return $data; - } + if ($data === $this->missingData) { + return $falseValues[0]; } - } catch (\Error $error) { - if ('Typed property Symfony\Component\Form\FormConfigBuilder::$type must not be accessed before initialization' === $error->getMessage()) { + + if (\in_array($data, $falseValues)) { return $data; } - - throw $error; } if (null === $data || $this->missingData === $data) { From 4dcd5a4c2badb1a6597d4e90eebe38779273d6a7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E2=80=9CFilip?= Date: Tue, 4 Oct 2022 19:45:16 +0200 Subject: [PATCH 13/13] [WIP] Code clarifications --- .../HttpFoundation/HttpFoundationRequestHandler.php | 5 ++++- .../Component/Form/Tests/AbstractRequestHandlerTest.php | 3 +-- 2 files changed, 5 insertions(+), 3 deletions(-) diff --git a/src/Symfony/Component/Form/Extension/HttpFoundation/HttpFoundationRequestHandler.php b/src/Symfony/Component/Form/Extension/HttpFoundation/HttpFoundationRequestHandler.php index 9c1bfc75ae70a..1471d19930b71 100644 --- a/src/Symfony/Component/Form/Extension/HttpFoundation/HttpFoundationRequestHandler.php +++ b/src/Symfony/Component/Form/Extension/HttpFoundation/HttpFoundationRequestHandler.php @@ -58,8 +58,11 @@ public function handleRequest(FormInterface $form, mixed $request = null) $data = $request->query->all(); } else { $missingData = $this->missingDataHandler->missingData; + $queryData = $request->query->all()[$name] ?? $missingData; - if ($missingData === $data = $this->missingDataHandler->handle($form, $request->query->all()[$name] ?? $missingData)) { + $data = $this->missingDataHandler->handle($form, $queryData); + + if ($missingData === $data) { // Don't submit GET requests if the form's name does not exist // in the request return; diff --git a/src/Symfony/Component/Form/Tests/AbstractRequestHandlerTest.php b/src/Symfony/Component/Form/Tests/AbstractRequestHandlerTest.php index 7011aa29c4591..9fdb2696ad0ff 100644 --- a/src/Symfony/Component/Form/Tests/AbstractRequestHandlerTest.php +++ b/src/Symfony/Component/Form/Tests/AbstractRequestHandlerTest.php @@ -78,8 +78,7 @@ public function testSubmitCheckboxInCollectionFormWithEmptyData($method) 'method' => $method, ]); - $this->setRequestData($method, [ - ]); + $this->setRequestData($method, []); $this->requestHandler->handleRequest($form, $this->request); pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

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:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy