8000 [9.x] Added argument transformNullToEmptyString to the functions old() and … by Loots-it · Pull Request #35853 · laravel/framework · GitHub
[go: up one dir, main page]

Skip to content

[9.x] Added argument transformNullToEmptyString to the functions old() and … #35853

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jan 12, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
Added argument transformNullToEmptyString to the functions old() and …
…getOldInput() and added tests
  • Loading branch information
Loots-it committed Jan 12, 2021
commit a071b7779ab1aef9339993bf1c850f0b728d877f
5 changes: 3 additions & 2 deletions src/Illuminate/Foundation/helpers.php
Original file line number Diff line number Diff line change
Expand Up @@ -526,11 +526,12 @@ function now($tz = null)
*
* @param string|null $key
* @param mixed $default
* @param bool $transformNullToEmptyString
* @return mixed
*/
function old($key = null, $default = null)
function old($key = null, $default = null, $transformNullToEmptyString = false)
{
return app('request')->old($key, $default);
return app('request')->old($key, $default, $transformNullToEmptyString);
}
< 8000 /td> }

Expand Down
11 changes: 9 additions & 2 deletions src/Illuminate/Session/Store.php
Original file line number Diff line number Diff line change
Expand Up @@ -248,11 +248,18 @@ public function hasOldInput($key = null)
*
* @param string|null $key
* @param mixed $default
* @param bool $transformNullToEmptyString
* @return mixed
*/
public function getOldInput($key = null, $default = null)
public function getOldInput($key = null, $default = null, $transformNullToEmptyString = false)
{
return Arr::get($this->get('_old_input', []), $key, $default);
$oldInputs = $this->get('_old_input', []);
$result = Arr::get($oldInputs, $key, $default);

if ($transformNullToEmptyString && is_null($result) && Arr::has($oldInputs, $key)) {
return '';
}
return $result;
}

/**
Expand Down
7 changes: 6 additions & 1 deletion tests/Session/SessionStoreTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -226,7 +226,7 @@ public function testOldInputFlashing()
{
$session = $this->getSession();
$session->put('boom', 'baz');
$session->flashInput(['foo' => 'bar', 'bar' => 0]);
$session->flashInput(['foo' => 'bar', 'bar' => 0, 'name' => null]);

$this->assertTrue($session->hasOldInput('foo'));
$this->assertSame('bar', $session->getOldInput('foo'));
Expand All @@ -239,6 +239,11 @@ public function testOldInputFlashing()
$this->assertSame('bar', $session->getOldInput('foo'));
$this->assertEquals(0, $session->getOldInput('bar'));
$this->assertFalse($session->hasOldInput('boom'));

$this->assertSame('default', $session->getOldInput('input', 'default'));
$this->assertSame('default', $session->getOldInput('input', 'default', true));
$this->assertSame('', $session->getOldInput('name', 'default', true));
$this->assertSame(null, $session->getOldInput('name', 'default'));
}

public function testDataFlashing()
Expand Down
0