bug #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 | #13962
| License       | MIT
| Doc PR        | -

Commits
-------

fa9fb5c Replace GET parameters when changed
This commit is contained in:
Fabien Potencier 2015-03-23 10:09:00 +01:00
commit 86e9b9409c
2 changed files with 18 additions and 3 deletions

View File

@ -197,9 +197,18 @@ class Form extends Link implements \ArrayAccess
{
$uri = parent::getUri();
if (!in_array($this->getMethod(), array('POST', 'PUT', 'DELETE', 'PATCH')) && $queryString = http_build_query($this->getValues(), null, '&')) {
$sep = false === strpos($uri, '?') ? '?' : '&';
$uri .= $sep.$queryString;
if (!in_array($this->getMethod(), array('POST', 'PUT', 'DELETE', 'PATCH'))) {
$query = parse_url($uri, PHP_URL_QUERY);
$currentParameters = array();
if ($query) {
parse_str($query, $currentParameters);
}
$queryString = http_build_query(array_merge($currentParameters, $this->getValues()), null, '&');
$pos = strpos($uri, '?');
$base = false === $pos ? $uri : substr($uri, 0, $pos);
$uri = rtrim($base.'?'.$queryString, '?');
}
return $uri;

View File

@ -573,6 +573,12 @@ class FormTest extends \PHPUnit_Framework_TestCase
array(),
'/foo?bar=bar&foo=foo',
),
array(
'replaces query values with the form values',
'<form action="/foo?bar=bar"><input type="text" name="bar" value="foo" /><input type="submit" /></form>',
array(),
'/foo?bar=foo',
),
array(
'returns an empty URI if the action is empty',
'<form><input type="submit" /></form>',