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.
7 Replies
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
dont store the total, have it computed based on the other fields?
with a placeholder instead of $set
its up to you
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?
yeah, as long as the fields are live they will rerender the placeholder content
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.
Hey @Emaz would you mind showcasing your placeholder solution please? I'm trying to do the exact same thing and currently utalising a similar nesting function to determine the $get scope correctly which is getting too complicated
Oh was actually really simple, ended up doing this which is far cleaner than nested scope gets per field
Placeholder::make('overview')
->hiddenLabel()
->content(function(Get $get){
$data = self::updateGrandTotal($get);
return view('forms.components.prod-quote-calculations')->with('data', $data);
})
Cheers for the insight!
Cheers for the insight!