alfredfriedrich
Using a calculate in a form field
Hello,
first time playing around with Ash and hitting a wall. I’m trying to use a calucate in a form field. Lets say I have an attribute :duration, :integer and an attribute :duration_unit, :duration_name in a resource, and a calculate :duration_option, expr(duration <> " " <> duration_unit).
If I try to use the duration_option in a form field, I’m only getting a generic error from AshPhoenix.Form.submit.
I’ve looked at the custom composable types, but I’m using AshSqlite and it does not support this.
I guess “I’m using it wrong”, could someone point me at the “Ash” direction, please? ![]()
Thanks alfred
Marked As Solved
zachdaniel
Calculations are likely not the fit here. You are likely looking for arguments. In your create action:
create :create do
accept [:email_address]
argument :duration_option, :string, allow_nil?: false
change SetDurationAttributes
end
defmodule SetDurationAttributes do
use Ash.Resource.Change
def change(changeset, _opts, _context) do
case parse_duration(changeset.arguments.duration_option) do
{:ok, duration, duration_unit} ->
Ash.Changeset.force_change_attributes(
changeset,
%{duration: duration, duration_unit: duration_unit}
)
{:error, error} ->
Ash.Changeset.add_error(changeset, error)
end
end
end
Also Liked
alfredfriedrich
Okay, I can see the benefits of using arguments and changes now. It encapsulates the parsing and converting logic into the resource (or into a Ash.Resource.Change module) and not into the Form. This is much clearer for me, and the prefered solution.
alfredfriedrich
I read a little more in the Ash Framework book and in chapter 2 they use the transform_params option to the form_to_create_resource call, so I tried it like this:
def mount(_, _, socket) do
form = MyApp.MyDomain.form_to_create_my_resource(
transform_params; fn _form, params, _context ->
parse_duration(params)
end
)
socket = assign(socket, :fom, to_form(form)
{:ok, socket}
end
This works for me as expected, as I understand it, if I’d use argument and change it would work on the Ash.Changeset, but as far as my example goes, working on the params directly is good for me.
I used the calculation too for the <select> form field value by the way.
Thanks again, this was fun playing around with
I will definitely use this on my next project.
Popular in Questions
Other popular topics
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #deployment
- #library
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #channels
- #elixirconf
- #exunit
- #discussion
- #code-sync
- #javascript
- #podcasts
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #elixir-ls
- #phoenix_html
- #iex
- #blog-post
- #graphql
- #genstage
- #ai
- #websockets
- #elixirconf-us
- #supervisor
- #advent-of-code
- #distillery
- #processes
- #api
- #forms
- #metaprogramming
- #security
- #hex









