Can I inject a variable amount of dropdowns based on reactive field value?

Hello, the use case is rather simple. I have a dropdown with some products. After selecting one I want to display a number of other dropdowns based on that selection. Might be 1 dropdown, might be 4. I don't know until the selection is made. Is that possible with form builder?
11 Replies
Dan Harrin
Dan Harrin2y ago
probably, but how are the values gonna be stored from those dropdowns?
Fally
FallyOP2y ago
The lifecycle hooks, on store or update i parse the fields
Dan Harrin
Dan Harrin2y ago
i dont get it json?
Fally
FallyOP2y ago
i'll try to explain better, here's a video of how I have it now, first drop down where i choose the product has reactive() on it and the second dropdown loads some options based on that particular product https://share.cleanshot.com/JMp9pcS0
CleanShot Cloud
CleanShot 2023-06-28 at 17.01.10
Video uploaded to CleanShot Cloud
Fally
FallyOP2y ago
You see that for attribute it has something like Tricou dama, alb, M I want that single select to be 3 different selects in this case some other product might require 2 selects instead of 3 i want to know if there's a way to programatically inject form components in the schema based on the selected product from the first dropdown
Dan Harrin
Dan Harrin2y ago
Group::make()->schema(function ($get) {
// $get('field') and return array
})
Group::make()->schema(function ($get) {
// $get('field') and return array
})
make sure the original field is reactive()
Fally
FallyOP2y ago
i will try this right now, thank you so much
Dan Harrin
Dan Harrin2y ago
you might need to add an afterStateUpdated() to the original field to $set() each select to null but see if that works first
Fally
FallyOP2y ago
yeah, those are small workarounds i can figure out, this was the main issue I didn't know how to tackle, thank you again Sorry to bother you again but I'm getting a weird error after creating the product. It didn't use to happen before doing what you suggested. It creates the product without issue but "addEagerConstraints() on null" error pops up and the stack trace isn't really helping. Do i need to somehow destroy those selects after the creation process is finalized?
Tables\Actions\CreateAction::make()
->using(function (Tables\Contracts\HasRelationshipTable $livewire, array $data): OrderDetail {
$data['order_id'] = $livewire->getRelationship()->getParent()->id;
$data['subtotal'] = round($data['qty'] * $data['unit_price'], 2);
$data['type'] = OrderDetail::TYPE_PRODUCT;

$product = OrderDetail::create($data);

// calculate totals
OrderHelper::updateOrderTotals($livewire->getRelationship()->getParent());

return $product;
}),
Tables\Actions\CreateAction::make()
->using(function (Tables\Contracts\HasRelationshipTable $livewire, array $data): OrderDetail {
$data['order_id'] = $livewire->getRelationship()->getParent()->id;
$data['subtotal'] = round($data['qty'] * $data['unit_price'], 2);
$data['type'] = OrderDetail::TYPE_PRODUCT;

$product = OrderDetail::create($data);

// calculate totals
OrderHelper::updateOrderTotals($livewire->getRelationship()->getParent());

return $product;
}),
Fally
FallyOP2y ago
if anyone runs into this, i fixed the error by refreshing the parent model like this ->after(function (Tables\Contracts\HasRelationshipTable $livewire) { $livewire->getRelationship()->getParent()->refresh();
}),
Want results from more Discord servers?
Add your server