Skip to content

Commit 4d2b3fe

Browse files
authored
Merge pull request #8736 from kenjis/fix-form-helper-type-error
fix: TypeError in form()
2 parents d344ab6 + e1cf987 commit 4d2b3fe

File tree

2 files changed

+26
-14
lines changed

2 files changed

+26
-14
lines changed

system/Helpers/form_helper.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ function form_open(string $action = '', $attributes = [], array $hidden = []): s
3131
{
3232
// If no action is provided then set to the current url
3333
if ($action === '') {
34-
$action = current_url(true);
34+
$action = (string) current_url(true);
3535
} // If an action is not a full URL then turn it into one
3636
elseif (! str_contains($action, '://')) {
3737
// If an action has {locale}

tests/system/Helpers/FormHelperTest.php

Lines changed: 25 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -101,21 +101,33 @@ public function testFormOpenWithoutAction(): void
101101
{
102102
$this->setRequest();
103103

104-
$before = (new Filters())->globals['before'];
105-
if (in_array('csrf', $before, true) || array_key_exists('csrf', $before)) {
106-
$Value = csrf_hash();
107-
$Name = csrf_token();
108-
$expected = <<<EOH
109-
<form action="http://example.com/index.php" name="form" id="form" method="POST" accept-charset="utf-8">
110-
<input type="hidden" name="{$Name}" value="{$Value}" style="display:none;">
104+
$expected = <<<'EOH'
105+
<form action="http://example.com/index.php" name="form" id="form" method="POST" accept-charset="utf-8">
111106

112-
EOH;
113-
} else {
114-
$expected = <<<'EOH'
115-
<form action="http://example.com/index.php" name="form" id="form" method="POST" accept-charset="utf-8">
107+
EOH;
108+
$attributes = [
109+
'name' => 'form',
110+
'id' => 'form',
111+
'method' => 'POST',
112+
];
113+
$this->assertSame($expected, form_open('', $attributes));
114+
}
116115

117-
EOH;
118-
}
116+
public function testFormOpenWithoutActionWithCSRF(): void
117+
{
118+
$this->setRequest();
119+
120+
// Sets csrf filter.
121+
$filters = config(Filters::class);
122+
$filters->globals['before'][] = 'csrf';
123+
service('filters')->initialize();
124+
125+
$Value = csrf_hash();
126+
$Name = csrf_token();
127+
$expected = <<<EOH
128+
<form action="http://example.com/index.php" name="form" id="form" method="POST" accept-charset="utf-8">
129+
<input type="hidden" name="{$Name}" value="{$Value}">
130+
EOH;
119131
$attributes = [
120132
'name' => 'form',
121133
'id' => 'form',

0 commit comments

Comments
 (0)