Livewire component with custom, dynamic form, struggling with select default values

I have this in my livewire component:
public function form(Form $form): Form
{
return $form->schema($this->getFormFields());
}

public function getFormFields()
{
if (empty($this->selectedTableHeaders)) return [];
$arr = [];
$i = 1;

foreach ($this->selectedTableHeaders as $key => $mapping) {

$defaults = $this->fieldColumnMap[$mapping['id']];
$options = $mapping['header_array'];

$arr[] = Section::make('Selected Header ' . $i)->description($key)->aside()->schema([
Grid::make()->columns(2)->schema([
Select::make($mapping['id'] . '-transaction_date')->label('Transaction Date')->default(array_search($defaults['transaction_date'], $options))->options($options),
...more selects similar to above...
]),
]);

$i++;
}

return $arr;
}
public function form(Form $form): Form
{
return $form->schema($this->getFormFields());
}

public function getFormFields()
{
if (empty($this->selectedTableHeaders)) return [];
$arr = [];
$i = 1;

foreach ($this->selectedTableHeaders as $key => $mapping) {

$defaults = $this->fieldColumnMap[$mapping['id']];
$options = $mapping['header_array'];

$arr[] = Section::make('Selected Header ' . $i)->description($key)->aside()->schema([
Grid::make()->columns(2)->schema([
Select::make($mapping['id'] . '-transaction_date')->label('Transaction Date')->default(array_search($defaults['transaction_date'], $options))->options($options),
...more selects similar to above...
]),
]);

$i++;
}

return $arr;
}
The above form is generated dynamically based on user selections in step 1 of a wizard. Step 2 is the above code. However, the default value isn't working on the select. I've tried adding $this->form->fill() to my render method, but it's not working. Since I don't know how many forms/fields will exist until the user makes a selection, I can't set the defaults inside of $this->form->fill(). In my blade file I have this:
@if ($currentStep == 1)
{{ $this->table }}
@endif

@if ($currentStep == 2)
{{ $this->form }}
@endif
@if ($currentStep == 1)
{{ $this->table }}
@endif

@if ($currentStep == 2)
{{ $this->form }}
@endif
Any ideas?
3 Replies
Jon Mason
Jon MasonOP8mo ago
Still struggling with this, hoping maybe someone has some ideas. This is the current iteration of the offending code. I just can't seem to set a default value for the select:
Select::make($mapping['id'] . '-transaction_date')
->label('Transaction Date')
->formatStateUsing(fn () => array_search($defaults['transaction_date'], $outputArr))
// ->afterStateUpdated(fn (Select $component) => $component->state(array_search($defaults['transaction_date'], $outputArr)))
->live()
->options($options)
->required(),
Select::make($mapping['id'] . '-transaction_date')
->label('Transaction Date')
->formatStateUsing(fn () => array_search($defaults['transaction_date'], $outputArr))
// ->afterStateUpdated(fn (Select $component) => $component->state(array_search($defaults['transaction_date'], $outputArr)))
->live()
->options($options)
->required(),
I've also tried ->afterStateHydrated(fn (Select $component) => $component->state(array_search($defaults['transaction_date'], $options)))
Dennis Koch
Dennis Koch8mo ago
Defaults should be passed through $this->form->fill() I think
Want results from more Discord servers?
Add your server