F
Filament10mo ago
nowak

Preserve data across form wizard steps

I have a 2 step form wizard, with a meal_type_id field in step 1, which I want to preserve for step 2. This is what I have tried so far. In the meal_type_id field, I have tried to set the $state to a selected_meal_type_id variable like so:
Select::make('meal_type_id')
->options(function (Get $get) {
$group = Group::find($get('group_id'));
return $group->mealTypes->pluck('name', 'id');
})
->live()
->afterStateUpdated(function (Set $set, $state) {
$set('selected_meal_type_id', $state);
})
->required(),
Select::make('meal_type_id')
->options(function (Get $get) {
$group = Group::find($get('group_id'));
return $group->mealTypes->pluck('name', 'id');
})
->live()
->afterStateUpdated(function (Set $set, $state) {
$set('selected_meal_type_id', $state);
})
->required(),
Then in my repeater function in step 2, I want to Get this data in a Select field:
public static function getItemsRepeater(): Repeater
{
return Repeater::make('productUserOrders')
->relationship()
->schema([
Select::make('product_id')
->label('Product')
->options(function (Get $get, $state) {
$mealTypeId = $get('selected_meal_type_id');
if(! $mealTypeId) {
return [];
}
$mealType = MealType::find($mealTypeId);
return $mealType->products->pluck('name', 'id');
})
->required()
->live()
->afterStateUpdated(fn ($state, Set $set) => $set('price', Product::find($state)?->price ?? 0))
->distinct()
->columnSpan([
'md' => 5,
]),
])
->hiddenLabel()
->columns([
'md' => 10,
])
->required();
}
public static function getItemsRepeater(): Repeater
{
return Repeater::make('productUserOrders')
->relationship()
->schema([
Select::make('product_id')
->label('Product')
->options(function (Get $get, $state) {
$mealTypeId = $get('selected_meal_type_id');
if(! $mealTypeId) {
return [];
}
$mealType = MealType::find($mealTypeId);
return $mealType->products->pluck('name', 'id');
})
->required()
->live()
->afterStateUpdated(fn ($state, Set $set) => $set('price', Product::find($state)?->price ?? 0))
->distinct()
->columnSpan([
'md' => 5,
]),
])
->hiddenLabel()
->columns([
'md' => 10,
])
->required();
}
But it seems like the data is not preserved from step 1 to step 2. What am I doing wrong?
3 Replies
nowak
nowakOP10mo ago
I have also tried to Get the meal_type_id directly, but I only get null on step 2.
nowak
nowakOP10mo ago
Wow, I read this documentation, but when I read it I thought parent meant fields within the repeater, so I didn't try this. Thank you so much!!
Want results from more Discord servers?
Add your server