FilamentF
Filament3y ago
qcol

afterStateUpdated and field changed by $set()

The documentation says that afterStateUpdated does not work when a field changes via $set(). Sometimes, however, instead of the user, the field is just changed by $set(). How to deal with this?

For example, I have qty, price and total fields. The price field is ->live() and has afterStateUpdated() in which I set the total field after changing the price value. However, in some situations, the price field is set by $set(). How then to update the total field?
Solution
You don’t inject it. Let’s say you have a quantity field and an item_price field and a total_price field.

Quantity and item_price are both set to reactive. That will force the form to re-render. So on the total_price field you can use afterStateUpdated() or afterStateHydrated() to $get(‘quantity) and $get(‘item_price), do the math and make the state of total_price equal to that value.
Was this page helpful?