motinska94's avatar

Volt component not selecting disabled select option as placeholder.

On my class based Volt component, I have the select input below :

<select class="form-select" name="product_id" wire:model.live='selectedProduct' required>

    <option value="" selected disabled>Select a product</option> {{-- It's not selecting this --}}

    @foreach ($this->categories() as $categoryName => $categoryProducts)
        <option disabled>{{ $categoryName }}</option>

        @foreach ($categoryProducts as $product)
            <option value="{{ $product['id'] }}">
                - {{ $product['name'] }}
            </option>
        @endforeach

    @endforeach
</select>

In the PHP part of the component, I am assigning selectedProduct to null with this line : public $selectedProduct = null;

So I want the first option (<option value="" disabled>Select a product</option>) to be selected when the form loads for the first time. But for some reason it automatically selects the first loaded product option on the list.

$this->categories() returns an array of product categories with products in each of them. It's a visual thing to group products by category name in the select input.

I tried :

  • Hard coding selected on the placeholder option.
  • Changing public $selectedProduct = null; with public $selectedProduct;
  • Changing wire:model.live with wire:model
  • Removing category title lines (<option disabled>{{ $categoryName }}</option>) so there's only one "null" option on the list.

And it's still selecting the first non-null option. Any idea how I can fix this?

0 likes
5 replies
ian_h's avatar

Can you select an item that's disabled?

motinska94's avatar

Can you not?

As far as I know that's how you make a placeholder for select inputs. You put selected disabled attributes on the first option, and make it say something like "Select product"

I know I have faced and solved this issue on a Livewire component before, but I don't remember how I did it, and since it was a client project I don't have access to the files anymore.

Snapey's avatar
Snapey
Best Answer
Level 122

your value on the element is an empty string, not null.

initialise your value the same, ie $value = '';

1 like
motinska94's avatar

Oh god, I knew it was stupid simple. Thanks a lot!

Snapey's avatar

good! please mark it solved by choosing best answer

Please or to participate in this conversation.