8000 merged branch pylebecq/2.2 (PR #8221) · symfony/symfony@1f26887 · GitHub
[go: up one dir, main page]

Skip to content

Commit 1f26887

Browse files
committed
merged branch pylebecq/2.2 (PR #8221)
This PR was merged into the 2.2 branch. Discussion ---------- [DomCrawler] Fixed a fatal error when setting a value in a malformed field name | Q | A | ------------- | --- | Bug fix? | yes | New feature? | no | BC breaks? | no | Deprecations? | no | Tests pass? | yes | Fixed tickets | | License | MIT | Doc PR | Hi, I found a case where a fatal error happen when trying to set a value in a form field with a malformed name. ``` Fatal error: Call to a member function setValue() on a non-object in /home/pierreyves/projects/symfony2/symfony/src/Symfony/Component/DomCrawler/FormFieldRegistry.php on line 128 Call Stack: 0.0001 231832 1. {main}() /home/pierreyves/projects/symfony2/symfony/vendor/phpunit/phpunit/composer/bin/phpunit:0 0.0035 688768 2. PHPUnit_TextUI_Command::main() /home/pierreyves/projects/symfony2/symfony/vendor/phpunit/phpunit/composer/bin/phpunit:63 0.0035 689000 3. PHPUnit_TextUI_Command->run() /home/pierreyves/projects/symfony2/symfony/vendor/phpunit/phpunit/PHPUnit/TextUI/Command.php:129 0.0224 2705448 4. PHPUnit_TextUI_TestRunner->doRun() /home/pierreyves/projects/symfony2/symfony/vendor/phpunit/phpunit/PHPUnit/TextUI/Command.php:176 0.0301 2997392 5. PHPUnit_Framework_TestSuite->run() /home/pierreyves/projects/symfony2/symfony/vendor/phpunit/phpunit/PHPUnit/TextUI/TestRunner.php:349 0.0477 3613296 6. PHPUnit_Framework_TestSuite->runTest() /home/pierreyves/projects/symfony2/symfony/vendor/phpunit/phpunit/PHPUnit/Framework/TestSuite.php:745 0.0477 3613296 7. PHPUnit_Framework_TestCase->run() /home/pierreyves/projects/symfony2/symfony/vendor/phpunit/phpunit/PHPUnit/Framework/TestSuite.php:775 0.0477 3613296 8. PHPUnit_Framework_TestResult->run() /home/pierreyves/projects/symfony2/symfony/vendor/phpunit/phpunit/PHPUnit/Framework/TestCase.php:776 0.0478 3614208 9. PHPUnit_Framework_TestCase->runBare() /home/pierreyves/projects/symfony2/symfony/vendor/phpunit/phpunit/PHPUnit/Framework/TestResult.php:648 0.0478 3630992 10. PHPUnit_Framework_TestCase->runTest() /home/pierreyves/projects/symfony2/symfony/vendor/phpunit/phpunit/PHPUnit/Framework/TestCase.php:831 0.0478 3631856 11. ReflectionMethod->invokeArgs() /home/pierreyves/projects/symfony2/symfony/vendor/phpunit/phpunit/PHPUnit/Framework/TestCase.php:976 0.0478 3631888 12. Symfony\Component\DomCrawler\Tests\FormTest->testSetValueOnMultiValuedFieldsWithMalformedName() /home/pierreyves/projects/symfony2/symfony/vendor/phpunit/phpunit/PHPUnit/Framework/TestCase.php:976 0.0481 3635560 13. Symfony\Component\DomCrawler\Form->offsetSet() /home/pierreyves/projects/symfony2/symfony/vendor/phpunit/phpunit/PHPUnit/Framework/TestCase.php:255 0.0481 3635560 14. Symfony\Component\DomCrawler\FormFieldRegistry->set() /home/pierreyves/projects/symfony2/symfony/src/Symfony/Component/DomCrawler/Form.php:320 ``` In this case, `FormFieldRegistry::getSegments('foo[bar')` method is called and it will return `array('foo')` only. Therefore the return of `FormFieldRegistry::get('foo[bar')` returns an array instead of a `FormField` and so the `setValue()` call happen on the array which leads to the fatal error. I tried to fix that. I don't know if it's the best way to do this so, as always, comments are welcome. Commits ------- bce6bd2 [DomCrawler] Fixed a fatal error when setting a value in a malformed field name.
2 parents 18f55ff + bce6bd2 commit 1f26887

File tree

2 files changed

+18
-2
lines changed

2 files changed

+18
-2
lines changed

src/Symfony/Component/DomCrawler/FormFieldRegistry.php

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -204,8 +204,12 @@ private function getSegments($name)
204204
{
205205
if (preg_match('/^(?P<base>[^[]+)(?P<extra>(\[.*)|$)/', $name, $m)) {
206206
$segments = array($m['base']);
207-
while (preg_match('/^\[(?P<segment>.*?)\](?P<extra>.*)$/', $m['extra'], $m)) {
208-
$segments[] = $m['segment'];
207+
while (!empty($m['extra'])) {
208+
if (preg_match('/^\[(?P<segment>.*?)\](?P<extra>.*)$/', $m['extra'], $m)) {
209+
$segments[] = $m['segment'];
210+
} else {
211+
throw new \InvalidArgumentException(sprintf('Malformed field path "%s"', $name));
212+
}
209213
}
210214

211215
return $segments;

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

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -247,6 +247,18 @@ public function testGetSetValue()
247247
}
248248
}
249249

250+
public function testSetValueOnMultiValuedFieldsWithMalformedName()
251+
{
252+
$form = $this->createForm('<form><input type="text" name="foo[bar]" value="bar" /><input type="text" name="foo[baz]" value="baz" /><input type="submit" /></form>');
253+
254+
try {
255+
$form['foo[bar'] = 'bar';
256+
$this->fail('->offsetSet() throws an \InvalidArgumentException exception if the name is malformed.');
257+
} catch (\InvalidArgumentException $e) {
258+
$this->assertTrue(true, '->offsetSet() throws an \InvalidArgumentException exception if the name is malformed.');
259+
}
260+
}
261+
250262
public function testOffsetUnset()
251263
{
252264
$form = $this->createForm('<form><input type="text" name="foo" value="foo" /><input type="submit" /></form>');

0 commit comments

Comments
 (0)
0