From bce6bd2858ab11e6d97435c7e86120e17219f767 Mon Sep 17 00:00:00 2001 From: Pierre-Yves LEBECQ Date: Sat, 8 Jun 2013 13:27:47 +0200 Subject: [PATCH] [DomCrawler] Fixed a fatal error when setting a value in a malformed field name. --- .../Component/DomCrawler/FormFieldRegistry.php | 8 ++++++-- src/Symfony/Component/DomCrawler/Tests/FormTest.php | 12 ++++++++++++ 2 files changed, 18 insertions(+), 2 deletions(-) diff --git a/src/Symfony/Component/DomCrawler/FormFieldRegistry.php b/src/Symfony/Component/DomCrawler/FormFieldRegistry.php index 5144ac739c..677a9aad31 100644 --- a/src/Symfony/Component/DomCrawler/FormFieldRegistry.php +++ b/src/Symfony/Component/DomCrawler/FormFieldRegistry.php @@ -204,8 +204,12 @@ class FormFieldRegistry { if (preg_match('/^(?P[^[]+)(?P(\[.*)|$)/', $name, $m)) { $segments = array($m['base']); - while (preg_match('/^\[(?P.*?)\](?P.*)$/', $m['extra'], $m)) { - $segments[] = $m['segment']; + while (!empty($m['extra'])) { + if (preg_match('/^\[(?P.*?)\](?P.*)$/', $m['extra'], $m)) { + $segments[] = $m['segment']; + } else { + throw new \InvalidArgumentException(sprintf('Malformed field path "%s"', $name)); + } } return $segments; diff --git a/src/Symfony/Component/DomCrawler/Tests/FormTest.php b/src/Symfony/Component/DomCrawler/Tests/FormTest.php index 93841e59c2..15af5a24ea 100644 --- a/src/Symfony/Component/DomCrawler/Tests/FormTest.php +++ b/src/Symfony/Component/DomCrawler/Tests/FormTest.php @@ -247,6 +247,18 @@ class FormTest extends \PHPUnit_Framework_TestCase } } + public function testSetValueOnMultiValuedFieldsWithMalformedName() + { + $form = $this->createForm('
'); + + try { + $form['foo[bar'] = 'bar'; + $this->fail('->offsetSet() throws an \InvalidArgumentException exception if the name is malformed.'); + } catch (\InvalidArgumentException $e) { + $this->assertTrue(true, '->offsetSet() throws an \InvalidArgumentException exception if the name is malformed.'); + } + } + public function testOffsetUnset() { $form = $this->createForm('
');