diff --git a/system/Helpers/form_helper.php b/system/Helpers/form_helper.php index cf55d06c7698..e505e65b70b8 100644 --- a/system/Helpers/form_helper.php +++ b/system/Helpers/form_helper.php @@ -69,6 +69,12 @@ function form_open(string $action = '', $attributes = [], array $hidden = []): s } // If an action is not a full URL then turn it into one elseif (strpos($action, '://') === false) { + // If an action has {locale} + if (strpos($action, '{locale}') !== false) + { + $action = str_replace('{locale}', Services::request()->getLocale(), $action); + } + $action = site_url($action); } diff --git a/tests/system/Helpers/FormHelperTest.php b/tests/system/Helpers/FormHelperTest.php index 7d5c96ba1678..87006ebe1515 100644 --- a/tests/system/Helpers/FormHelperTest.php +++ b/tests/system/Helpers/FormHelperTest.php @@ -55,6 +55,29 @@ public function testFormOpenBasic() $this->assertEquals($expected, form_open('foo/bar', $attributes)); } + // ------------------------------------------------------------------------ + public function testFormOpenHasLocale() + { + $config = new App(); + $config->baseURL = ''; + $config->indexPage = 'index.php'; + $request = Services::request($config); + $request->uri = new URI('http://example.com/'); + + Services::injectMock('request', $request); + $expected = << + +EOH; + + $attributes = [ + 'name' => 'form', + 'id' => 'form', + 'method' => 'POST', + ]; + $this->assertEquals($expected, form_open('{locale}/foo/bar', $attributes)); + } + // ------------------------------------------------------------------------ public function testFormOpenWithoutAction() { diff --git a/user_guide_src/source/helpers/form_helper.rst b/user_guide_src/source/helpers/form_helper.rst index 7c0e9645cf3a..1f8085bf5f5b 100644 --- a/user_guide_src/source/helpers/form_helper.rst +++ b/user_guide_src/source/helpers/form_helper.rst @@ -75,6 +75,15 @@ The following functions are available:
+ You can also add {locale} like the following:: + + echo form_open('{locale}/email/send'); + + The above example would create a form that points to your base URL plus the current request locale with + "email/send" URI segments, like this:: + + + **Adding Attributes** Attributes can be added by passing an associative array to the second @@ -90,13 +99,13 @@ The following functions are available: The above examples would create a form similar to this:: - + If CSRF filter is turned on `form_open()` will generate CSRF field at the beginning of the form. You can specify ID of this field by passing csrf_id as one of the $attribute array: - + form_open('/u/sign-up', ['csrf_id' => 'my-id']); - + will return: - +