Multiselect issue #2195
-
Hi, using selects is working fine but when I use select with the multiple method, it's saving the values but it's not showing the selected values when I go to the edit page again. The db field is json and I can see the json string in the db field.
|
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 4 replies
-
there is a tiny bug I would say, which is that the multiple selects values which I also stored as a json array into the database are stored as strings. This resulted the in_array function within the blade to fail. What I did was creating a custom select which only uses my custom view with the bug fix that casts the key to be a string. <?php
declare(strict_types=1);
namespace App\Orchid\Fields;
use Orchid\Screen\Fields\Select;
/**
* Class Custom Select.
**/
class CustomSelect extends Select
{
/**
* @var string
*/
protected $view = 'fields.custom.select';
} note the string type cast @component($typeForm, get_defined_vars())
<div data-controller="select">
<select {{ $attributes }}>
@foreach($options as $key => $option)
<option value="{{$key}}"
@isset($value)
@if (is_array($value) && in_array((string) $key, $value, true)) selected
@elseif (isset($value[$key]) && $value[$key] == $option) selected
@elseif ($key === $value) selected
@endif
@endisset
>{{$option}}</option>
@endforeach
</select>
</div>
@endcomponent |
Beta Was this translation helpful? Give feedback.
there is a tiny bug I would say, which is that the multiple selects values which I also stored as a json array into the database are stored as strings. This resulted the in_array function within the blade to fail. What I did was creating a custom select which only uses my custom view with the bug fix that casts the key to be a string.
note the string type cast