Skip to content
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
27 changes: 26 additions & 1 deletion docs/helpers/form_helper.md
Original file line number Diff line number Diff line change
Expand Up @@ -461,6 +461,13 @@ echo form_dropdown('shirts', $options, 'large', $js);

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

##### Automatic Selection from POST

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).

!!! note "Note"
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.

#### form_multiselect([$name = ''[, $options = [][, $selected = [][, $extra = '']]]])

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

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.

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[].
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[]`.

If `$selected` is omitted, selections are automatically inferred from `$_POST['shirts']`:

```php
<?php

// If $_POST['shirts'] contains ['small', 'large']:
echo form_multiselect('shirts[]', $options);
/*
* Would produce:
* <select name="shirts[]" multiple="multiple">
* <option value="small" selected="selected">Small Shirt</option>
* <option value="med">Medium Shirt</option>
* <option value="large" selected="selected">Large Shirt</option>
* <option value="xlarge">Extra Large Shirt</option>
* </select>
*/
```

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

Expand Down
16 changes: 7 additions & 9 deletions system/Helpers/form_helper.php
Original file line number Diff line number Diff line change
Expand Up @@ -293,16 +293,14 @@ function form_dropdown($data = '', $options = [], $selected = [], $extra = ''):

// If no selected state was submitted we will attempt to set it automatically
if ($selected === []) {
Comment thread
michalsn marked this conversation as resolved.
Resolved
$superglobals = service('superglobals');
if (is_array($data)) {
$postValue = $superglobals->post($data['name'] ?? '');
if (isset($data['name']) && $postValue !== null) {
$selected = [$postValue];
}
} else {
$postValue = $superglobals->post($data);
$name = is_array($data) ? ($data['name'] ?? '') : $data;
$name = str_ends_with($name, '[]') ? substr($name, 0, -2) : $name;

if ($name !== '') {
$postValue = service('superglobals')->post($name);

if ($postValue !== null) {
$selected = [$postValue];
$selected = is_array($postValue) ? $postValue : [$postValue];
}
}
}
Expand Down
53 changes: 53 additions & 0 deletions tests/system/Helpers/FormHelperTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -588,6 +588,36 @@ public function testFormDropdownInferred(): void
service('superglobals')->unsetPost('cars');
}

public function testFormDropdownInferredArray(): void
{
$options = [
'Swedish Cars' => [
'volvo' => 'Volvo',
'saab' => 'Saab',
],
'German Cars' => [
'mercedes' => 'Mercedes',
'audi' => 'Audi',
],
];
$expected = <<<EOH
<select name="cars" multiple="multiple">
<optgroup label="Swedish Cars">
<option value="volvo" selected="selected">Volvo</option>
<option value="saab">Saab</option>
</optgroup>
<optgroup label="German Cars">
<option value="mercedes">Mercedes</option>
<option value="audi" selected="selected">Audi</option>
</optgroup>
</select>\n
EOH;
service('superglobals')->setPost('cars', ['volvo', 'audi']);
$this->assertSame($expected, form_dropdown('cars', $options));
$this->assertSame($expected, form_dropdown(['name' => 'cars'], $options));
service('superglobals')->unsetPost('cars');
}

public function testFormDropdownWithSelectedAttribute(): void
{
$expected = <<<'EOH'
Expand Down Expand Up @@ -684,6 +714,29 @@ public function testFormMultiselectArrayData(): void
$this->assertSame($expected, form_multiselect($data));
}

public function testFormMultiselectInferred(): void
{
$expected = <<<EOH
<select name="shirts[]" multiple="multiple">
<option value="small">Small Shirt</option>
<option value="med" selected="selected">Medium Shirt</option>
<option value="large" selected="selected">Large Shirt</option>
<option value="xlarge">Extra Large Shirt</option>
</select>\n
EOH;
$options = [
'small' => 'Small Shirt',
'med' => 'Medium Shirt',
'large' => 'Large Shirt',
'xlarge' => 'Extra Large Shirt',
];

service('superglobals')->setPost('shirts', ['med', 'large']);
$this->assertSame($expected, form_multiselect('shirts[]', $options));
$this->assertSame($expected, form_multiselect(['name' => 'shirts[]', 'options' => $options]));
service('superglobals')->unsetPost('shirts');
}

public function testFormFieldset(): void
{
$expected = <<<EOH
Expand Down
Loading