[DomCrawler] Fixed a fatal error when setting a value in a malformed field name.

This commit is contained in:
Pierre-Yves LEBECQ 2013-06-08 13:27:47 +02:00
parent 18f55ffe5e
commit bce6bd2858
2 changed files with 18 additions and 2 deletions

View File

@ -204,8 +204,12 @@ class FormFieldRegistry
{ {
if (preg_match('/^(?P<base>[^[]+)(?P<extra>(\[.*)|$)/', $name, $m)) { if (preg_match('/^(?P<base>[^[]+)(?P<extra>(\[.*)|$)/', $name, $m)) {
$segments = array($m['base']); $segments = array($m['base']);
while (preg_match('/^\[(?P<segment>.*?)\](?P<extra>.*)$/', $m['extra'], $m)) { while (!empty($m['extra'])) {
$segments[] = $m['segment']; if (preg_match('/^\[(?P<segment>.*?)\](?P<extra>.*)$/', $m['extra'], $m)) {
$segments[] = $m['segment'];
} else {
throw new \InvalidArgumentException(sprintf('Malformed field path "%s"', $name));
}
} }
return $segments; return $segments;

View File

@ -247,6 +247,18 @@ class FormTest extends \PHPUnit_Framework_TestCase
} }
} }
public function testSetValueOnMultiValuedFieldsWithMalformedName()
{
$form = $this->createForm('<form><input type="text" name="foo[bar]" value="bar" /><input type="text" name="foo[baz]" value="baz" /><input type="submit" /></form>');
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() public function testOffsetUnset()
{ {
$form = $this->createForm('<form><input type="text" name="foo" value="foo" /><input type="submit" /></form>'); $form = $this->createForm('<form><input type="text" name="foo" value="foo" /><input type="submit" /></form>');