Skip to content

Commit 287a433

Browse files
committed
[Form] Fixed memory leak in FormValidator
1 parent a4469e3 commit 287a433

File tree

2 files changed

+8
-13
lines changed

2 files changed

+8
-13
lines changed

Extension/Validator/Constraints/FormValidator.php

Lines changed: 7 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,9 @@
2323
class FormValidator extends ConstraintValidator
2424
{
2525
/**
26-
* @var \SplObjectStorage
26+
* @var array
2727
*/
28-
private static $clickedButtons;
28+
private static $clickedButtons = array();
2929

3030
/**
3131
* @var ServerParams
@@ -52,15 +52,9 @@ public function validate($form, Constraint $constraint)
5252
return;
5353
}
5454

55-
if (null === static::$clickedButtons) {
56-
static::$clickedButtons = new \SplObjectStorage();
57-
}
58-
5955
// If the form was previously validated, remove it from the cache in
6056
// case the clicked button has changed
61-
if (static::$clickedButtons->contains($form)) {
62-
static::$clickedButtons->detach($form);
63-
}
57+
unset(self::$clickedButtons[spl_object_hash($form)]);
6458

6559
/* @var FormInterface $form */
6660
$config = $form->getConfig();
@@ -188,16 +182,17 @@ private static function allowDataWalking(FormInterface $form)
188182
private static function getValidationGroups(FormInterface $form)
189183
{
190184
$root = $form->getRoot();
185+
$rootHash = spl_object_hash($root);
191186

192187
// Determine the clicked button of the complete form tree
193-
if (!static::$clickedButtons->contains($root)) {
188+
if (!array_key_exists($rootHash, self::$clickedButtons)) {
194189
// Only call findClickedButton() once to prevent an exponential
195190
// runtime
196191
// https://github.com/symfony/symfony/issues/8317
197-
static::$clickedButtons->attach($root, self::findClickedButton($root));
192+
self::$clickedButtons[$rootHash] = self::findClickedButton($root);
198193
}
199194

200-
$button = static::$clickedButtons->offsetGet($root);
195+
$button = self::$clickedButtons[$rootHash];
201196

202197
if (null !== $button) {
203198
$groups = $button->getConfig()->getOption('validation_groups');

Tests/Extension/Validator/Constraints/FormValidatorPerformanceTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ public function testValidationPerformance()
3838

3939
$builder = $this->factory->createBuilder('form');
4040

41-
for ($i = 0; $i < 100; ++$i) {
41+
for ($i = 0; $i < 40; ++$i) {
4242
$builder->add($i, 'form');
4343

4444
$builder->get($i)

0 commit comments

Comments
 (0)