Skip to content

Commit 4d6773e

Browse files
authored
form_open enhancments (reusing existing code)
Allows to pass the ID of CSRF field as a key of form_open $attribute arr. `form_open(['csrf_id' => 'id-foo-bar']);`
1 parent 1c01ed6 commit 4d6773e

1 file changed

Lines changed: 10 additions & 5 deletions

File tree

system/Helpers/form_helper.php

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,12 @@ function form_open(string $action = '', $attributes = [], array $hidden = []): s
6565
$action = site_url($action);
6666
}
6767

68+
if(is_array($attributes) && array_key_exists('csrf_id', $attributes))
69+
{
70+
$csrf_id = $attributes['csrf_id'];
71+
unset($attributes['csrf_id']);
72+
}
73+
6874
$attributes = stringify_attributes($attributes);
6975

7076
if (stripos($attributes, 'method=') === false)
@@ -82,17 +88,16 @@ function form_open(string $action = '', $attributes = [], array $hidden = []): s
8288
// Add CSRF field if enabled, but leave it out for GET requests and requests to external websites
8389
$before = Services::filters()->getFilters()['before'];
8490

85-
if ((in_array('csrf', $before) || array_key_exists('csrf', $before)) && strpos($action, base_url()) !== false && ! stripos($form, 'method="get"')
86-
)
91+
if ((in_array('csrf', $before) || array_key_exists('csrf', $before)) && strpos($action, base_url()) !== false && ! stripos($form, 'method="get"'))
8792
{
88-
$hidden[csrf_token()] = csrf_hash();
93+
$form .= csrf_field($csrf_id ?? null);
8994
}
9095

9196
if (is_array($hidden))
9297
{
9398
foreach ($hidden as $name => $value)
9499
{
95-
$form .= '<input type="hidden" name="' . $name . '" value="' . esc($value, 'html') . '" style="display: none;" />' . "\n";
100+
$form .= form_hidden($name, $value);
96101
}
97102
}
98103

@@ -167,7 +172,7 @@ function form_hidden($name, $value = '', bool $recursing = false): string
167172

168173
if (! is_array($value))
169174
{
170-
$form .= '<input type="hidden" name="' . $name . '" value="' . esc($value, 'html') . "\" />\n";
175+
$form .= '<input type="hidden" name="' . $name . '" value="' . esc($value, 'html') . "\" style="display:none;" />\n";
171176
}
172177
else
173178
{

0 commit comments

Comments
 (0)