Skip to content

Commit 252a386

Browse files
committed
refactor(Helpers): simplify name extraction and document POST inference
1 parent 20a7022 commit 252a386

2 files changed

Lines changed: 32 additions & 9 deletions

File tree

‎docs/helpers/form_helper.md‎

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -461,6 +461,13 @@ echo form_dropdown('shirts', $options, 'large', $js);
461461

462462
If the array passed as `$options` is a multidimensional array, then `form_dropdown()` will produce an <optgroup> with the array key as the label.
463463

464+
##### Automatic Selection from POST
465+
466+
If no value is passed to `$selected` (or if it is an empty array), `form_dropdown()` and `form_multiselect()` will attempt to infer the selected option(s) directly from the current request's `$_POST` data using the field name (trailing `[]` brackets are automatically stripped).
467+
468+
!!! note "Note"
469+
This fallback only inspects `$_POST` from the current request. It does not restore old input after a redirect with `redirect()->withInput()`. To restore submitted values after a redirect, pass `set_value()` or `old()` to `$selected` explicitly.
470+
464471
#### form_multiselect([$name = ''[, $options = [][, $selected = [][, $extra = '']]]])
465472

466473
- **Parameters**
@@ -475,7 +482,25 @@ If the array passed as `$options` is a multidimensional array, then `form_dropdo
475482

476483
Lets you create a standard multiselect field. The first parameter will contain the name of the field, the second parameter will contain an associative array of options, and the third parameter will contain the value or values you wish to be selected.
477484

478-
The parameter usage is identical to using `form_dropdown()` above, except, of course, that the name of the field will need to use POST array syntax, e.g., foo[].
485+
The parameter usage is identical to using `form_dropdown()` above, except, of course, that the name of the field will need to use POST array syntax, e.g., `shirts[]`.
486+
487+
If `$selected` is omitted, selections are automatically inferred from `$_POST['shirts']`:
488+
489+
```php
490+
<?php
491+
492+
// If $_POST['shirts'] contains ['small', 'large']:
493+
echo form_multiselect('shirts[]', $options);
494+
/*
495+
* Would produce:
496+
* <select name="shirts[]" multiple="multiple">
497+
* <option value="small" selected="selected">Small Shirt</option>
498+
* <option value="med">Medium Shirt</option>
499+
* <option value="large" selected="selected">Large Shirt</option>
500+
* <option value="xlarge">Extra Large Shirt</option>
501+
* </select>
502+
*/
503+
```
479504

480505
#### form_fieldset([$legend_text = ''[, $attributes = []]])
481506

‎system/Helpers/form_helper.php‎

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -293,14 +293,12 @@ function form_dropdown($data = '', $options = [], $selected = [], $extra = ''):
293293

294294
// If no selected state was submitted we will attempt to set it automatically
295295
if ($selected === []) {
296-
$superglobals = service('superglobals');
297-
if (is_array($data)) {
298-
$postValue = $superglobals->post(preg_replace('/\[\]$/', '', $data['name'] ?? ''));
299-
if (isset($data['name']) && $postValue !== null) {
300-
$selected = is_array($postValue) ? $postValue : [$postValue];
301-
}
302-
} else {
303-
$postValue = $superglobals->post(preg_replace('/\[\]$/', '', $data));
296+
$name = is_array($data) ? ($data['name'] ?? '') : $data;
297+
$name = str_ends_with($name, '[]') ? substr($name, 0, -2) : $name;
298+
299+
if ($name !== '') {
300+
$postValue = service('superglobals')->post($name);
301+
304302
if ($postValue !== null) {
305303
$selected = is_array($postValue) ? $postValue : [$postValue];
306304
}

0 commit comments

Comments
 (0)