Skip to content

Commit eeb3a02

Browse files
committed
- Automatically adding old values to tags
Signed-off-by: Arushad Ahmed <dash-8x@hotmail.com>
1 parent 0a8bce6 commit eeb3a02

File tree

3 files changed

+17
-3
lines changed

3 files changed

+17
-3
lines changed

docs/basic-usage/select2.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,8 @@ This would add `data-minimum-results-for-search="Infinity"` to the select elemen
4343

4444
# Enabling tags
4545

46-
To enable the user to add their own options to the select, enable the `tags` feature of Select2 by using the `tags` attribute.
46+
To enable the user to add their own options to the select, enable the `tags` feature of Select2 by using the `tags` attribute.
47+
When you use the `tags` option, any old values will automatically get added to the options if there's a validation error.
4748

4849
```html
4950
<x-forms::select2 name="categories" :options="$options" multiple tags />

src/Views/Components/Select.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ class Select extends Component
3535
public bool $showLabel;
3636
public string $syncFieldName;
3737
public bool $isAjax;
38+
public bool $tags;
3839

3940
/**
4041
* Create a new component instance.
@@ -63,6 +64,7 @@ public function __construct(
6364
string $syncFieldName = '',
6465
string $nameField = '',
6566
string $idField = '',
67+
bool $tags = false,
6668
public string $inlineLabelClass = '',
6769
public string $inlineInputClass = '',
6870
public string $formGroupClass = '',
@@ -112,8 +114,20 @@ public function __construct(
112114
$this->floating = $floating;
113115
$this->required = $required;
114116
$this->inline = $inline;
117+
$this->tags = $tags;
115118

116119
$this->options = $options instanceof BuilderContract ? $this->getOptionsFromQueryBuilder($options) : $options;
120+
121+
// include old values for tags
122+
if ($this->tags && is_array($this->options)) {
123+
$old_value = Arr::wrap(old($inputName));
124+
125+
foreach ($old_value as $value) {
126+
if (! array_key_exists($value, $this->options)) {
127+
$this->options[$value] = $value;
128+
}
129+
}
130+
}
117131
}
118132

119133
public function getOptionsFromQueryBuilder(BuilderContract $query): array

src/Views/Components/Select2.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44

55
class Select2 extends Select
66
{
7-
public bool $tags;
87
public bool $allowClear;
98
public bool $isFirst;
109
public bool $hideSearch;
@@ -85,6 +84,7 @@ public function __construct(
8584
syncFieldName: $syncFieldName,
8685
nameField: $nameField,
8786
idField: $idField,
87+
tags: $tags,
8888
inlineLabelClass: $inlineLabelClass,
8989
inlineInputClass: $inlineInputClass,
9090
formGroupClass: $formGroupClass,
@@ -97,7 +97,6 @@ public function __construct(
9797
$this->ajaxUrl = $ajaxUrl;
9898
$this->selectedUrl = $selectedUrl;
9999
$this->filterField = $filterField;
100-
$this->tags = $tags;
101100
$this->hideSearch = $hideSearch;
102101
$this->allowClear = $allowClear;
103102
$this->fallback = $fallback;

0 commit comments

Comments
 (0)