Calculations and AshPhoenix.Form
First post! So thanks @Zach Daniel for this awesome library. Looks really promising!
I'm working on a side project where I have to store employee contracts. For each contract I store the employment (as factor between 0 and 1) and the number of vacation days per year (when working full time).
I'm using
AshPhoenix.Form.for_action()
and LiveView to create a form (see screenshot) where the user can enter the employment as percentage (argument emp_percent
) and the number of vacation days (attribute vac_days
) and the hint should show the calculated effective vacation days based on the employment factor.
I'm already validating the form on change but I would like vac_days_eff
to be recalculated each time (so I can show it in the hint below the field). I might also be a bit confused about action attributes/arguments. Could this be solved using a private action argument?5 Replies
I already got it working with the following helper but I'm wondering if this is the right way:
Sorry, forgot to add the screenshot

Ah, yeah so there aren't any helpers for recalculating on submit, but there is a helper for running calculations without needing the underlying record. Here is how you'd use it:
So then in your UI you could do something like this:
To clean this up a bit, you can use the code interface
And then the above looks like this:
Thanks for the quick reply! It works! 🙂
🥳 that helper is relatively new, so there may be some rough edges. The main thing to remember: it can only do expressions that can be done without doing a sql query
(or other data layer operations)
So if you want to use that helper, you can't have things like
fragment/1
, for example