File tree Expand file tree Collapse file tree 3 files changed +17
-3
lines changed
Expand file tree Collapse file tree 3 files changed +17
-3
lines changed Original file line number Diff line number Diff 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 />
Original file line number Diff line number Diff 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
Original file line number Diff line number Diff line change 44
55class 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 ;
You can’t perform that action at this time.
0 commit comments