Update fields of a nested AshPhoenix.Form on change of a different field

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!

: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) 

  assign(socket, :form, new_form)
1 Like

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(
        %{"_target" => ["u_form", "user_tenant_roles", role_index, "time_period_id"], "u_form" => params},
      ) 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
        |> 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"))
        |> put_in(["user_tenant_roles", role_index, "start_at"], nil)
        |> put_in(["user_tenant_roles", role_index, "end_at"], nil)

    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")
            Map.merge(nested_form.params, %{"time_period_id" => nil, "start_at" => nil, "end_at" => nil})

        AshPhoenix.Form.validate(nested_form, params)

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

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.

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…


1 Like