Skip to content

Commit dacacf4

Browse files
committed
fix: widen accepted types in set_value()'s $default
1 parent da4e508 commit dacacf4

4 files changed

Lines changed: 52 additions & 15 deletions

File tree

‎system/Helpers/form_helper.php‎

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
* the LICENSE file that was distributed with this source code.
1212
*/
1313

14+
use CodeIgniter\HTTP\IncomingRequest;
1415
use CodeIgniter\Validation\Exceptions\ValidationException;
1516
use Config\App;
1617
use Config\Validation;
@@ -547,27 +548,25 @@ function form_close(string $extra = ''): string
547548

548549
if (! function_exists('set_value')) {
549550
/**
550-
* Form Value
551-
*
552551
* Grabs a value from the POST array for the specified field so you can
553552
* re-populate an input field or textarea
554553
*
555-
* @param string $field Field name
556-
* @param list<string>|string $default Default value
557-
* @param bool $htmlEscape Whether to escape HTML special characters or not
554+
* @param string $field Field name
555+
* @param list<string>|scalar|Stringable|null $default Default value
556+
* @param bool $htmlEscape Whether to escape HTML special characters or not
558557
*
559558
* @return list<string>|string
560559
*/
561560
function set_value(string $field, $default = '', bool $htmlEscape = true): array|string
562561
{
562+
/** @var IncomingRequest $request */
563563
$request = service('request');
564564

565-
// Try any old input data we may have first
566-
$value = $request->getOldInput($field);
567-
568-
$value ??= $request->getPost($field) ?? $default;
565+
$value = $request->getOldInput($field)
566+
?? $request->getPost($field)
567+
?? (is_array($default) ? $default : (string) $default);
569568

570-
return ($htmlEscape) ? esc($value) : $value;
569+
return $htmlEscape ? esc($value) : $value;
571570
}
572571
}
573572

‎tests/system/Helpers/FormHelperTest.php‎

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,12 @@
2222
use Config\Filters;
2323
use Config\Services;
2424
use PHPUnit\Framework\Attributes\BackupGlobals;
25+
use PHPUnit\Framework\Attributes\DataProvider;
2526
use PHPUnit\Framework\Attributes\Group;
2627
use PHPUnit\Framework\Attributes\PreserveGlobalState;
2728
use PHPUnit\Framework\Attributes\RunInSeparateProcess;
2829
use PHPUnit\Framework\Attributes\WithoutErrorHandler;
30+
use Stringable;
2931

3032
/**
3133
* @internal
@@ -867,6 +869,38 @@ public function testSetValue(): void
867869
$this->assertSame('baz', set_value('foo', 'baz'));
868870
}
869871

872+
#[DataProvider('provideSetValueCastsNonArrayDefaultToString')]
873+
public function testSetValueCastsNonArrayDefaultToString(mixed $default, string $expected): void
874+
{
875+
$this->assertSame($expected, set_value('foo', $default));
876+
}
877+
878+
/**
879+
* @return iterable<string, array{mixed, string}>
880+
*/
881+
public static function provideSetValueCastsNonArrayDefaultToString(): iterable
882+
{
883+
$stringable = self::createStub(Stringable::class);
884+
$stringable->method('__toString')->willReturn('<bar>');
885+
886+
yield 'int' => [1, '1'];
887+
888+
yield 'float' => [1.5, '1.5'];
889+
890+
yield 'true' => [true, '1'];
891+
892+
yield 'false' => [false, ''];
893+
894+
yield 'null' => [null, ''];
895+
896+
yield 'stringable' => [$stringable, '&lt;bar&gt;'];
897+
}
898+
899+
public function testSetValueKeepsArrayDefault(): void
900+
{
901+
$this->assertSame(['a', 'b'], set_value('foo', ['a', 'b']));
902+
}
903+
870904
public function testSetSelect(): void
871905
{
872906
$_SESSION['_ci_old_input']['post']['foo'] = 'bar';

‎user_guide_src/source/changelogs/v4.7.6.rst‎

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,13 +38,17 @@ Bugs Fixed
3838
is used instead. Values that cannot be converted to a string, such as objects
3939
not implementing ``Stringable``, now throw a ``FormatException`` instead of a
4040
fatal ``Error``.
41-
- **Security:** Fixed a bug where a valid JSON request body that is not an object
42-
(for example a JSON array) was corrupted after CSRF verification when the
43-
token was sent via the ``X-CSRF-TOKEN`` header.
41+
- **Helpers:** Fixed a regression in v4.7.5 where ``set_value()`` threw a
42+
``TypeError`` when the default value was not a string or an array, such as
43+
an integer, a float, ``null``, or a ``Stringable`` object. Such values are
44+
now converted to a string.
4445
- **HTTP:** Fixed a bug where ``IncomingRequest::isSecure()`` did not
4546
recognize ``https`` in the ``X-Forwarded-Proto`` header from a trusted
4647
proxy when the value contained uppercase letters or surrounding
4748
whitespace, or was represented as a single-element array.
49+
- **Security:** Fixed a bug where a valid JSON request body that is not an object
50+
(for example a JSON array) was corrupted after CSRF verification when the
51+
token was sent via the ``X-CSRF-TOKEN`` header.
4852

4953
See the repo's
5054
`CHANGELOG.md <https://github.com/codeigniter4/CodeIgniter4/blob/develop/CHANGELOG.md>`_

‎user_guide_src/source/helpers/form_helper.rst‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -452,10 +452,10 @@ The following functions are available:
452452
.. php:function:: set_value($field[, $default = ''[, $html_escape = true]])
453453
454454
:param string $field: Field name
455-
:param string $default: Default value
455+
:param mixed $default: Default value. An array is returned as is, any other value is converted to a string.
456456
:param bool $html_escape: Whether to turn off HTML escaping of the value
457457
:returns: Field value
458-
:rtype: string
458+
:rtype: array|string
459459

460460
Permits you to set the value of an input or textarea element. You must
461461
supply the field name via the first parameter of the function. The

0 commit comments

Comments
 (0)