Skip to content

Commit b560240

Browse files
committed
add ability to replace {locale} to request->getLocale() in form_open(action)
1 parent ca3ed2f commit b560240

2 files changed

Lines changed: 29 additions & 0 deletions

File tree

system/Helpers/form_helper.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,12 @@ function form_open(string $action = '', $attributes = [], array $hidden = []): s
6969
} // If an action is not a full URL then turn it into one
7070
elseif (strpos($action, '://') === false)
7171
{
72+
// If an action has {locale}
73+
if (strpos($action, '{locale}') !== false)
74+
{
75+
$action = str_replace('{locale}', Services::request()->getLocale(), $action);
76+
}
77+
7278
$action = site_url($action);
7379
}
7480

tests/system/Helpers/FormHelperTest.php

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,29 @@ public function testFormOpenBasic()
5555
$this->assertEquals($expected, form_open('foo/bar', $attributes));
5656
}
5757

58+
// ------------------------------------------------------------------------
59+
public function testFormOpenHasLocale()
60+
{
61+
$config = new App();
62+
$config->baseURL = '';
63+
$config->indexPage = 'index.php';
64+
$request = Services::request($config);
65+
$request->uri = new URI('http://example.com/');
66+
67+
Services::injectMock('request', $request);
68+
$expected = <<<EOH
69+
<form action="http://example.com/index.php/en/foo/bar" name="form" id="form" method="POST" accept-charset="utf-8">
70+
71+
EOH;
72+
73+
$attributes = [
74+
'name' => 'form',
75+
'id' => 'form',
76+
'method' => 'POST',
77+
];
78+
$this->assertEquals($expected, form_open('{locale}/foo/bar', $attributes));
79+
}
80+
5881
// ------------------------------------------------------------------------
5982
public function testFormOpenWithoutAction()
6083
{

0 commit comments

Comments
 (0)