Dynamically created form schema setting other field value ignores what user changes as value
I'm going to try to explain this. I have a form in a relation manager. Based on a dropdown selection I build some extra fields using Forms\Components\Group::make()->schema(function ($get, $set, $context) {}
For instance, in my case, i have a product selector and then i build some dropdowns with choices based on that product. These choices affect price so I use $set('unit_price', $price) in the form schema that i mentioned above to set the field value. unit_price is a normal defined form field, it's not dynamically created or anything. Problem is, if i edit the field value, manually, afterwards the data that is passed to the update function is what i set with $set, it ignores the actual current user inputed field value.
I will attach a video where I will demonstrate that for entries where I don't have to use $set to set price, the data changes.
https://share.cleanshot.com/fxkZdXxn
23 Replies
Hi, can you show me your code pls ?
Hi @hugomyb , here's the dynamic form schema i'm creating
The problem is with the $set() line. I can't show all the code because discord has a character limit
Here's where I check the data for updating.
So the problem is that when you change the price manually, you don't get the new value of the price you've just changed? It keeps the $set value, right ?
Yes
I may have an idea, but I'm not sure.
Try to add the
->afterStateUpdated()
method, something like that, what does the dd return? :
Unfortunately it still shows the initial value
Where did you put the afterStateUpdated?
Try on Group::make
Or, what does return dd here :
on group it doesn't trigger
it's the initial value as well
It's weird, in the afterStateUpdated, the dd($state) should return the live modified value
my point exactly, i even updated to the latest version but this seems like a bug to me
Acutally I've no idea what the problem is, sorry...
thanks for your help anyway
i'm sorry to tag you in this @Dan Harrin but is there anyway you can help here? I've tried everything, i don't know if this is a bug or not.
you need to also use
->live()
if you are going to use afterStateUpdated()
Sorry but where do i need to use live() ? I can't find any reference in the docs and i tried it on the unit_price form field and the group
im using v2 not v3
It’s the same in v2 expect it’s ->reactive() instead of live.
If i use reactive on the field as soon as i change anything in it it gets overriden by the $set value. I’ve tried it before
CleanShot 2023-09-15 at 10.48.19
Video uploaded to CleanShot Cloud
So i managed to fix this by setting the product search dropdown to reactive and adding to it afterStateUpdated where I use $set() to set the unit price. For some reason, using $set() inside the Group schema triggers that weird read only problem.