lardcanoe

lardcanoe

I have a User resource with has_many roles.

The role has a time_period field, so when I show the array of roles using input_for, each role has a dropdown/select for the time_period.

When the user changes a role’s time_period I want to adjust 2 other fields of that role.

I am isolating the handle_event for when that time_period select changes. But from there, I don’t know what to do in the handle_event to change other fields, and have that reflected in the AshForm as well as in the UI.

Changing the params of the handle_event to have the data I want, and passing that into AshPhoenix.Form.validate(user_form, params) does nothing.

I tried using AshPhoenix.Form.update_form with a path to the role, but it isn’t clear what do in the function you supply.

Also, sometimes they might be editing an existing role from the db, or a new role.

Thank you!

Showing Posts 1 to 5

zachdaniel

zachdaniel

Creator of Ash

:wave: changing the params should work, not sure why it wouldn’t. You’re assigning the form after validating it again right?

As for using update_form, try something like this:

def handle_event("your_thing", %{"value" => value}, socket) do
  # path to your form can also be the form path, i.e `form[path][to][your][form]`
  # and can include integers to refer to an index
  new_form = AshPhoenix.Form.update_form([:path, :to, :your, :form], fn nested_form ->
    params = Map.merge(nested_form.params, %{"your" => "new params"})
    AshPhoenix.Form.validate(nested_form, params) 
  end)

  assign(socket, :form, new_form)
end
lardcanoe

lardcanoe OP

Thank you. That got it working, though still some oddities.

First, I had to muck with the form’s entire params, validate those, and then pass to the update_form call.

Second, this might be a Phoenix thing, but if I disable the input of the 2 date fields, then they don’t get saved. When a time period is set, I want to show the user the start/end but not let them change it.

(Note, I chose some hardcoded dates as an example, instead the time period’s actual)

“Working” version:

  def handle_event(
        "validate",
        %{"_target" => ["u_form", "user_tenant_roles", role_index, "time_period_id"], "u_form" => params},
        socket
      ) do
    time_period_id = get_in(params, ["user_tenant_roles", role_index, "time_period_id"])

    path = "u_form[user_tenant_roles][#{role_index}]"

    main_params =
      if time_period_id != "" do
        params
        |> put_in(["user_tenant_roles", role_index, "start_at"], Date.from_iso8601!("2019-01-01"))
        |> put_in(["user_tenant_roles", role_index, "end_at"], Date.from_iso8601!("2020-01-01"))
      else
        params
        |> put_in(["user_tenant_roles", role_index, "start_at"], nil)
        |> put_in(["user_tenant_roles", role_index, "end_at"], nil)
      end

    user_form =
      socket.assigns.user_form
      |> AshPhoenix.Form.validate(main_params)
      |> AshPhoenix.Form.update_form(path, fn nested_form ->
        params =
          if time_period_id != "" do
            Map.merge(nested_form.params, %{
              "time_period_id" => time_period_id,
              "start_at" => Date.from_iso8601!("2019-01-01"),
              "end_at" => Date.from_iso8601!("2020-01-01")
            })
          else
            Map.merge(nested_form.params, %{"time_period_id" => nil, "start_at" => nil, "end_at" => nil})
          end

        AshPhoenix.Form.validate(nested_form, params)
      end)

    {:noreply, assign(socket, user_form: user_form)}
  end
zachdaniel

zachdaniel

Creator of Ash

Interesting…surprising that you also had to do that for the outer params.

For the disabled inputs thing, my guess would be that if you disable them but want their input values to still be sent that you’d show a disabled input and use a hidden input for the actual value? Bit strange though.

EDIT: it seems like if you’re doing the outer bit that should be all that you need TBH.

lardcanoe

lardcanoe OP

Went with just futzing with the entire form’s params approach. Couldn’t get it working with just update_form.

And thank you for the tip on using hidden fields. That was exactly what I needed.

I never had to use embedded forms in plain phoenix, but I gotta say the way Ash handles it, and invokes actions on those embedded resources is really impressive. Took an hour to grok initially, but now that I know it, gonna really take advantage of this new power! :slight_smile:

You need to create a GPT for Ash that Co-pilot takes advantage of somehow. Train it on all these answers in elixirforum…

Cheers!

user20230119

user20230119

Do you have example code on what you ended up doing?

— All posts loaded —

Where Next? Top

Trending in Questions Top

stjefim
Hello! Suppose you are building workflow (order / task / payment) processing system with the following requirements: Each workflow con...
New
jonnycharles
I’m in search of an Elixir library that offers PDF generation capabilities similar to Ruby’s Prawn. While there have been discussions abo...
New
spammy
I’m looking to build a personal workflow to quickly deploy web applications written in elixir/phoenix, for local consumption (ie not on t...
New
dli
Before I dive in myself, did anyone successfully sprinkle Hologram into their existing LiveView app? Looking for hints regarding: Addi...
New
roeland
Kia ora, We have been using elixir-google-api to connect to Google Drive. However, with the updates to Tesla due to CVEs this is now bro...
New
bottlenecked
Hi all, I wanted to ask how the community is dealing with post-release steps. Today we have Ecto migrations, which make sure that the db...
New
rahultumpala
Hello, I have an Elixir backend that implements a custom protocol over TCP. I want to load test the backend and assess the performance o...
New

Other Trending Topics Top

JesseHerrick
Hey, I’m Jesse and I’m the main contributor behind Dexter, a full-featured, lightning-fast Elixir LSP optimized for large codebases. It s...
New
jimsynz
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
mcass19
ExRatatui lets you cook up rich terminal UIs in Elixir, powered by Rust’s ratatui via Rustler NIFs. Build interactive terminal applicatio...
New
Damirados
Hello everyone. After busy few months I am happy to announce v0.1.0 of Emerge & Solve. They are GUI (Emerge) and State management (S...
New
netoum
Corex is an accessible, unstyled UI component library for Phoenix that integrates Zag.js state machines using Vanilla JavaScript and Live...
New
ausimian
Emily is an Elixir library that runs Nx computations on Apple’s MLX. Install it as the default Nx backend and Nx, defn, Axon, Nx.Serving,...
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews