Skip to content

Commit 85e4f1a

Browse files
committed
Allow options of optional form fields to only define an id fixes #162
1 parent cace099 commit 85e4f1a

File tree

4 files changed

+22
-4
lines changed

4 files changed

+22
-4
lines changed

src/Form/Fields/SharpFormAutocompleteField.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -300,7 +300,7 @@ public function toArray(): array
300300
"placeholder" => $this->placeholder,
301301
"localValues" => $this->isLocal() && $this->dynamicAttributes
302302
? self::formatDynamicOptions($this->localValues, count($this->dynamicAttributes[0]["path"]))
303-
: ($this->isLocal() ? self::formatOptions($this->localValues) : []),
303+
: ($this->isLocal() ? self::formatOptions($this->localValues, $this->itemIdAttribute) : []),
304304
"itemIdAttribute" => $this->itemIdAttribute,
305305
"searchKeys" => $this->localSearchKeys,
306306
"remoteEndpoint" => $this->remoteEndpoint,

src/Form/Fields/SharpFormSelectField.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -208,7 +208,7 @@ public function toArray(): array
208208
array_merge([
209209
"options" => $this->dynamicAttributes
210210
? self::formatDynamicOptions($this->options, count($this->dynamicAttributes[0]["path"]))
211-
: self::formatOptions($this->options),
211+
: self::formatOptions($this->options, $this->idAttribute),
212212
"multiple" => $this->multiple,
213213
"clearable" => $this->clearable,
214214
"display" => $this->display,

src/Form/Fields/Utils/SharpFormFieldWithOptions.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,10 @@ trait SharpFormFieldWithOptions
99

1010
/**
1111
* @param array|Collection $options
12+
* @param string $idAttribute
1213
* @return array
1314
*/
14-
protected static function formatOptions($options)
15+
protected static function formatOptions($options, $idAttribute = "id")
1516
{
1617
if(! sizeof($options)) {
1718
return [];
@@ -20,7 +21,7 @@ protected static function formatOptions($options)
2021
$options = collect($options);
2122

2223
if((is_array($options->first()) || is_object($options->first()))
23-
&& isset(((array)$options->first())["id"])) {
24+
&& isset(((array)$options->first())[$idAttribute])) {
2425
// We assume that we already have ["id", "label"] in this case
2526
return $options->all();
2627
}

tests/Unit/Form/Fields/SharpFormSelectFieldTest.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,23 @@ function we_can_define_options_as_a_id_label_array()
117117
);
118118
}
119119

120+
/** @test */
121+
function we_can_define_options_as_a_custom_array()
122+
{
123+
$formField = $this->getDefaultSelect([
124+
["key" => "Elem-1"],
125+
["key" => "Elem-2"],
126+
])->setIdAttribute("key");
127+
128+
$this->assertArrayContainsSubset(
129+
["options" => [
130+
["key" => "Elem-1"],
131+
["key" => "Elem-2"],
132+
]],
133+
$formField->toArray()
134+
);
135+
}
136+
120137
/** @test */
121138
function we_can_define_localized_options()
122139
{

0 commit comments

Comments
 (0)