Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add ability to replace {locale} to request->getLocale() in form_open('action') #2790

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions system/Helpers/form_helper.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand Down
23 changes: 23 additions & 0 deletions tests/system/Helpers/FormHelperTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
<form action="http://example.com/index.php/en/foo/bar" name="form" id="form" method="POST" accept-charset="utf-8">

EOH;

$attributes = [
'name' => 'form',
'id' => 'form',
'method' => 'POST',
];
$this->assertEquals($expected, form_open('{locale}/foo/bar', $attributes));
}

// ------------------------------------------------------------------------
public function testFormOpenWithoutAction()
{
Expand Down
17 changes: 13 additions & 4 deletions user_guide_src/source/helpers/form_helper.rst
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,15 @@ The following functions are available:

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

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::

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

**Adding Attributes**

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

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

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:

<form action="/u/sign-up" method="post" accept-charset="utf-8">
<input type="hidden" id="my-id" name="csrf_field" value="964ede6e0ae8a680f7b8eab69136717d" />

Expand Down