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

support arity 3 cast functions #166

Merged
merged 1 commit into from Jun 3, 2023
Merged

support arity 3 cast functions #166

merged 1 commit into from Jun 3, 2023

Conversation

SteffenDE
Copy link
Contributor

I'm trying to implement ordering a has_many association following the documentation (https://hexdocs.pm/ecto/3.10.1/Ecto.Changeset.html#cast_assoc/3-sorting-and-deleting-from-many-collections). As this lead to some problems that were fixed by elixir-ecto/ecto#4178 (not released yet), I switched to using the current ecto main branch and modified my code according to the new documentation, adding a cast function with arity three, that receives the index as third parameter. This works fine, but not when used in a phoenix form.
When using it together with Phoenix.Component's inputs_for/1, it fails with an error like this:

** (FunctionClauseError) no function clause matching in Phoenix.HTML.FormData.Ecto.Changeset.to_changeset/4

     The following arguments were given to Phoenix.HTML.FormData.Ecto.Changeset.to_changeset/4:
     
         # 1
         %Comment{__meta__: #Ecto.Schema.Metadata<:built, "comments">, id: nil, body: "data1", position: nil}
     
         # 2
         nil
     
         # 3
         Comment
     
         # 4
         &Comment.changeset_with_position/3
     
     Attempted function clauses (showing 4 out of 4):
     
         defp to_changeset(%Ecto.Changeset{} = changeset, parent_action, _module, _cast)
         defp to_changeset(%{} = data, parent_action, _module, cast) when is_function(cast, 2)
         defp to_changeset(%{} = data, parent_action, _module, {module, func, arguments} = mfa) when is_atom(module) and is_atom(func) and is_list(arguments)
         defp to_changeset(%{} = data, parent_action, _module, nil)

So here's a PR that tries to add support for arity three cast functions. I'm not completely sure if I implemented this right, but it seems to work with the added test and also in my Phoenix app.
If I'm not on the right track here, see this as a bug report instead :)

elixir-ecto/ecto#4178 added support for using
an arity three cast function. This commit adds support to phoenix_ecto
as well.
@josevalim josevalim merged commit ec62653 into phoenixframework:main Jun 3, 2023
2 checks passed
@josevalim
Copy link
Member

💚 💙 💜 💛 ❤️

@SteffenDE SteffenDE deleted the arity_3_changeset_fun branch June 3, 2023 16:31
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants