F
Filament5w ago
Emaz

Setting/getting nested data form wide

Am I doing this wrong? Over complicating? I found it pretty challenging to do calculations using form data that includes both root level and nested data . I finally came up with this scheme but it feels complex. Am I missing something? calcTotal is the nethod to recalculate and is triggered all over the palce from various root level and nested fields. glass_type is a root selector, so I pass 0 as the 1st argument to indicate no nesting. Since hingecuts is one schema down, I pass a 1 to indicate the nesting level. The calculation code must also account for the dot notation parent name, fabrication. hingecuts but this depends on where the calculation is called from. This works but it's pretty complex. Is this how it's done? My form has LOTS of this nesting and any field can trigger a recalculation that needs all the data.
Select::make('glass_type')
->afterStateUpdated(function ($state, Forms\Set $set, Forms\Get $get) {
static::calcTotal(0, $set, $get, "glass_type");
}),

Grid::make(6)
->relationship('fabrication')
->schema([
TextInput::make('hingecuts')->numeric()->label("Hinges")
->afterStateUpdated(function ($state, Forms\Set $set, Forms\Get $get) {
static::calcTotal(1, $set, $get, "hingecuts");
})


public static function calcTotal($nestUp, $set, $get, $calledFrom = null)
{

$path = $nestUp > 0 ? str_repeat('../', $nestUp) : '';

// Fetch root-level fields using the conditional path
$glass_type = $get($path . "glass_type");
$glass_options = $get($path . "glass_options");
$panels = $get($path . "panels"); // root level repeater
$hardware = $get($path . "hardware"); // root level repeater

$hingecuts = $get($calledFrom === 'hingecuts' ? "hingecuts" : $path . "fabrication.hingecuts");

// etc
Select::make('glass_type')
->afterStateUpdated(function ($state, Forms\Set $set, Forms\Get $get) {
static::calcTotal(0, $set, $get, "glass_type");
}),

Grid::make(6)
->relationship('fabrication')
->schema([
TextInput::make('hingecuts')->numeric()->label("Hinges")
->afterStateUpdated(function ($state, Forms\Set $set, Forms\Get $get) {
static::calcTotal(1, $set, $get, "hingecuts");
})


public static function calcTotal($nestUp, $set, $get, $calledFrom = null)
{

$path = $nestUp > 0 ? str_repeat('../', $nestUp) : '';

// Fetch root-level fields using the conditional path
$glass_type = $get($path . "glass_type");
$glass_options = $get($path . "glass_options");
$panels = $get($path . "panels"); // root level repeater
$hardware = $get($path . "hardware"); // root level repeater

$hingecuts = $get($calledFrom === 'hingecuts' ? "hingecuts" : $path . "fabrication.hingecuts");

// etc
No description
Solution:
dont store the total, have it computed based on the other fields?
Jump to solution
6 Replies
Emaz
Emaz5w ago
I'm kind of feeling like I over-complicated this. If not, what is everyone else doing? Can we simplify this in V4? Is nobody else doing this kind of thing? There must be an easier way. @Dan Harrin ?
Solution
Dan Harrin
Dan Harrin5w ago
dont store the total, have it computed based on the other fields?
Dan Harrin
Dan Harrin5w ago
with a placeholder instead of $set its up to you
Emaz
Emaz5w ago
Oh good point. I am already using a placeholder. So you mean move all the totaling over there in once place. I guess that simplifies things because now the nesting will be all from one place? So the gets that need "../../" will at least be consistent. So I DID over complicate by doing the calculations at each field that changes, thus creating the nesting that varies. Better to do it all at the placeholder element. That's what you're saying, right?
Dan Harrin
Dan Harrin5w ago
yeah, as long as the fields are live they will rerender the placeholder content
Emaz
Emaz5w ago
I KNEW I was doing it wrong LOL. Thank you Dan. This would make a great tutorial or video series - I wish I had the time to do one. I just suspect others might make the same mistake in doing the calculations. My initial thought was to do it right at the field that changed. That can get real complicated real fast.