Skip to content

Commit 3490e53

Browse files
authored
Merge pull request #2790 from samsonasik/add-replace-locale-in-form-open
add ability to replace {locale} to request->getLocale() in form_open('action')
2 parents 7845edc + a2f1d82 commit 3490e53

3 files changed

Lines changed: 42 additions & 4 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
{

user_guide_src/source/helpers/form_helper.rst

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,15 @@ The following functions are available:
7575

7676
<form method="post" accept-charset="utf-8" action="http://example.com/index.php/email/send">
7777

78+
You can also add {locale} like the following::
79+
80+
echo form_open('{locale}/email/send');
81+
82+
The above example would create a form that points to your base URL plus the current request locale with
83+
"email/send" URI segments, like this::
84+
85+
<form method="post" accept-charset="utf-8" action="http://example.com/index.php/en/email/send">
86+
7887
**Adding Attributes**
7988

8089
Attributes can be added by passing an associative array to the second
@@ -90,13 +99,13 @@ The following functions are available:
9099
The above examples would create a form similar to this::
91100

92101
<form method="post" accept-charset="utf-8" action="http://example.com/index.php/email/send" class="email" id="myform">
93-
102+
94103
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:
95-
104+
96105
form_open('/u/sign-up', ['csrf_id' => 'my-id']);
97-
106+
98107
will return:
99-
108+
100109
<form action="/u/sign-up" method="post" accept-charset="utf-8">
101110
<input type="hidden" id="my-id" name="csrf_field" value="964ede6e0ae8a680f7b8eab69136717d" />
102111

0 commit comments

Comments
 (0)