Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Union type in :array does not work properly on type change #211

Open
revati opened this issue Jun 15, 2024 · 9 comments
Open

Union type in :array does not work properly on type change #211

revati opened this issue Jun 15, 2024 · 9 comments
Labels
bug Something isn't working

Comments

@revati
Copy link
Contributor

revati commented Jun 15, 2024

im fallowing this guide: https://hexdocs.pm/ash_phoenix/union-forms.html
but my union type is in list. Im get error:

[error] GenServer #PID<0.1780.0> terminating
** (KeyError) key :form_keys not found in: [
  #AshPhoenix.Form<
    resource: MyResourceWrapper,
    action: :update,
    type: :update,
    params: %{"_union_type" => "static"},
...
]
@revati revati added the bug Something isn't working label Jun 15, 2024
@zachdaniel
Copy link
Contributor

I will address this early next week. If you have time to put together a test/repro that will help 🙇

@zachdaniel zachdaniel moved this to Up Next in Ash Framework Roadmap Jun 15, 2024
@zachdaniel zachdaniel moved this from Up Next to In Progress in Ash Framework Roadmap Jun 17, 2024
@zachdaniel
Copy link
Contributor

So we have tests for this behavior, and as far as I can tell it works, so I'll need to see some sample code or a reproduction to go any further with it.

@adampointer
Copy link

Hi. I have the exact same bug. I am stumped but it is probably me doing something stupid.

It is the same as the OP, I have an array of union types and when I try and change type as per the tutorial on Union Forms, I get this error from forms.ex.

@zachdaniel
Copy link
Contributor

Are you on the latest version of ash_phoenix and ash? Some fixes were made recently to both.

@adampointer
Copy link

I am on the latest release ash phoenix v2.1.2

I can invite you to my repo if that's going to help

@zachdaniel
Copy link
Contributor

We can that, yes. Please be sure to provide very clear reproduction steps and/or a failing test illustrating the issue.

@adampointer
Copy link

Awesome. I will get a test case written after the weekend.

@adampointer
Copy link

Hey. I fixed this on my own. The issue I had was from following the examples on this page https://hexdocs.pm/ash_phoenix/union-forms.html.

In this event handler:

def handle_event("type-changed", %{"_target" => path} = params, socket) do
  new_type = get_in(params, path)
  # The last part of the path in this case is the field name
  path = :lists.droplast(path)

  form =
    socket.assigns.form
    |> AshPhoenix.Form.remove_form(path)
    |> AshPhoenix.Form.add_form(path, params: %{"_union_type" => new_type})

  {:noreply, assign(socket, :form, form)}
end

path was a list of strings like ["my_form", "content", "0"]. This was causing the above error to be thrown from the AshPhoenix.Form.add_form function. When I set the path as a string instead, it worked.

def handle_event("type-changed", %{"_target" => path} = params, socket) do
    new_type = get_in(params, path)
    index =
      path
      |> :lists.droplast()
      |> List.last()

    form =
      socket.assigns.form
      |> AshPhoenix.Form.remove_form("my_form[content][#{index}]")
      |> AshPhoenix.Form.add_form("my_form[content]",
        params: %{"_union_type" => new_type}
      )

    {:noreply, assign(socket, :form, form)}
  end

The one issue I have now, is that if I have more than one item in the list of subforms, when I change the type of the first item, it changes the type of all subsequent items too. Although that's not really a blocker, I might just disable the type select field once a new subform has been added. I am not sure this is the optimal fix for this.

@zachdaniel
Copy link
Contributor

Hm....this does still seem like something we should fix internally. Especially given that you aren't able to have a mixed list of types. If you have a chance to reproduce in a test that would be awesome :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

3 participants