alfredfriedrich

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? :slight_smile:

Thanks alfred

Marked As Solved

zachdaniel

zachdaniel

Creator of Ash

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

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

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 :smiley: I will definitely use this on my next project.

Where Next?

Popular in Questions Top

RisingFromAshes
I’ve read in another post that it may be possible with a router helper - but I couldn’t find an appropriate one, and tbh, I’m still just ...
New
9mm
I am constructing a JSON object (map) and I need to conditionally set a field. I’m trying to write proper elixir-way code… and I’m at a l...
New
komlanvi
Hi everyone, I was playing with phoenix liveView but I run into an issue. I have a form and want to validate each input text when the te...
New
bsollish-terakeet
Credo is smart enough to check for (something like) this: assert length(the_list) == 0 with this response: Checking if an enum is empt...
New
shijith.k
I am trying to start a new phoenix project with elixir 1.9, but mix phx.new does not work. It says that ** (Mix) The task "phx.new" could...
New
aalberti333
As the title describes, I’m trying to run Enum.map() over a list of key/value pairs, where the value is a map. My data looks like this: ...
New
pmjoe
I have a relationship of love and hate with Elixir. Lots of things are just absolutely right, but there are some things that are kind of ...
New

Other popular topics Top

KronicDeth
Elixir plugin for JetBrain’s IntelliJ Platform (including Rubymine) This is a plugin that adds support for Elixir to JetBrains IntelliJ...
289 36820 110
New
nobody
Hi! In PHP: $_SERVER[‘SERVER_ADDR’] - in Elixir? Searched the docs for ip address and the web, no good results. Thanks!
New
Brian
What is the proper way to load a module from a file in to IEX? In the python world, doing something like this pretty standard: from ....
New
dokuzbir
I want to highlight html closing tags when i click a html tag. That works in .html files but doesnt work for html.eex templates. How can...
New
msaraiva
Surface is an experimental library built on top of Phoenix LiveView and its new LiveComponent API that aims to provide a more declarative...
564 44265 214
New
AstonJ
Posting this to see if we can make things easier for people to get into Neovim. If you use Neovim and have a favourite distro please let ...
New

We're in Beta

About us Mission Statement