diff --git a/src/Symfony/Component/DomCrawler/FormFieldRegistry.php b/src/Symfony/Component/DomCrawler/FormFieldRegistry.php
index 5144ac739cae0..677a9aad3100f 100644
--- a/src/Symfony/Component/DomCrawler/FormFieldRegistry.php
+++ b/src/Symfony/Component/DomCrawler/FormFieldRegistry.php
@@ -204,8 +204,12 @@ private function getSegments($name)
{
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 93841e59c2dd6..15af5a24ea6f6 100644
--- a/src/Symfony/Component/DomCrawler/Tests/FormTest.php
+++ b/src/Symfony/Component/DomCrawler/Tests/FormTest.php
@@ -247,6 +247,18 @@ public function testGetSetValue()
}
}
+ 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('');