Skip to content

Commit 86e9b94

Browse files
committed
bug symfony#14010 Replace GET parameters when changed in form (WouterJ)
This PR was merged into the 2.3 branch. Discussion ---------- Replace GET parameters when changed in form | Q | A | ------------- | --- | Bug fix? | yes | New feature? | no | BC breaks? | no | Deprecations? | no | Tests pass? | yes | Fixed tickets | symfony#13962 | License | MIT | Doc PR | - Commits ------- fa9fb5c Replace GET parameters when changed
2 parents e5e27af + fa9fb5c commit 86e9b94

File tree

2 files changed

+18
-3
lines changed

2 files changed

+18
-3
lines changed

src/Symfony/Component/DomCrawler/Form.php

+12-3
Original file line numberDiff line numberDiff line change
@@ -197,9 +197,18 @@ public function getUri()
197197
{
198198
$uri = parent::getUri();
199199

200-
if (!in_array($this->getMethod(), array('POST', 'PUT', 'DELETE', 'PATCH')) && $queryString = http_build_query($this->getValues(), null, '&')) {
201-
$sep = false === strpos($uri, '?') ? '?' : '&';
202-
$uri .= $sep.$queryString;
200+
if (!in_array($this->getMethod(), array('POST', 'PUT', 'DELETE', 'PATCH'))) {
201+
$query = parse_url($uri, PHP_URL_QUERY);
202+
$currentParameters = array();
203+
if ($query) {
204+
parse_str($query, $currentParameters);
205+
}
206+
207+
$queryString = http_build_query(array_merge($currentParameters, $this->getValues()), null, '&');
208+
209+
$pos = strpos($uri, '?');
210+
$base = false === $pos ? $uri : substr($uri, 0, $pos);
211+
$uri = rtrim($base.'?'.$queryString, '?');
203212
}
204213

205214
return $uri;

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

+6
Original file line numberDiff line numberDiff line change
@@ -573,6 +573,12 @@ public function provideGetUriValues()
573573
array(),
574574
'/foo?bar=bar&foo=foo',
575575
),
576+
array(
577+
'replaces query values with the form values',
578+
'<form action="/foo?bar=bar"><input type="text" name="bar" value="foo" /><input type="submit" /></form>',
579+
array(),
580+
'/foo?bar=foo',
581+
),
576582
array(
577583
'returns an empty URI if the action is empty',
578584
'<form><input type="submit" /></form>',

0 commit comments

Comments
 (0)