Skip to content

Commit 991e65c

Browse files
committed
[DomCrawler] Throw an exception if a form field path is incomplete.
1 parent db24362 commit 991e65c

File tree

2 files changed

+29
-2
lines changed

2 files changed

+29
-2
lines changed

src/Symfony/Component/DomCrawler/FormFieldRegistry.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -124,13 +124,15 @@ public function has($name)
124124
public function set($name, $value)
125125
{
126126
$target = &$this->get($name);
127-
if (!is_array($value) || $target instanceof Field\ChoiceFormField) {
127+
if ((!is_array($value) && $target instanceof Field\FormField) || $target instanceof Field\ChoiceFormField) {
128128
$target->setValue($value);
129-
} else {
129+
} elseif (is_array($value)) {
130130
$fields = self::create($name, $value);
131131
foreach ($fields->all() as $k => $v) {
132132
$this->set($k, $v);
133133
}
134+
} else {
135+
throw new \InvalidArgumentException(sprintf('Cannot set value on a compound field "%s".', $name));
134136
}
135137
}
136138

src/Symfony/Component/DomCrawler/Tests/FormTest.php

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -787,6 +787,31 @@ public function testFormRegistrySetValues()
787787
));
788788
}
789789

790+
/**
791+
* @expectedException \InvalidArgumentException
792+
* @expectedExceptionMessage Cannot set value on a compound field "foo[bar]".
793+
*/
794+
public function testFormRegistrySetValueOnCompoundField()
795+
{
796+
$registry = new FormFieldRegistry();
797+
$registry->add($this->getFormFieldMock('foo[bar][baz]'));
798+
799+
$registry->set('foo[bar]', 'fbb');
800+
}
801+
802+
/**
803+
* @expectedException InvalidArgumentException
804+
* @expectedExceptionMessage Unreachable field "0"
805+
*/
806+
public function testFormRegistrySetArrayOnNotCompoundField()
807+
{
808+
809+
$registry = new FormFieldRegistry();
810+
$registry->add($this->getFormFieldMock('bar'));
811+
812+
$registry->set('bar', array('baz'));
813+
}
814+
790815
public function testDifferentFieldTypesWithSameName()
791816
{
792817
$dom = new \DOMDocument();

0 commit comments

Comments
 (0)