michaelherold

michaelherold

Copying errors into the form during `inputs_for`?

Why does Phoenix.HTML.FormData.Ecto.Changeset.form_for_errors/1 only copy errors into the form when the action is set to :insert, :update, :delete, or :replace? I understand not copying the errors on :ignore, but the nil clause is interesting to me.

A little explanation of what I’m trying to do:

defmodule MyApp.MyDomain.MySchema do
  embedded_schema do
    embeds_one :foo, Foo
    embeds_one :bar, Bar
  end

  @doc """
  This used in my Phoenix controller as shown below.
  """
  def changeset(model, attrs) do
    model
    |> cast(attrs, [])
    |> cast_embed(:foo)
    |> cast_embed(:bar)
  end
end
<%= form_for @changeset, my_path(@conn, :create), fn form -> %>
  <%= inputs_for form, :foo, fn foo_form -> %>
  <% end %>
  <%= inputs_for form, :bar, fn bar_form -> %>
  <% end %>
<% end %>

This schema is basically a compatibility layer that gets transformed into an Ecto.Multi for insertion. Since Ecto.Multi has no implementation for Phoenix.HTML.FormData, this is the way I chose to go about working the problem.

Because the changeset I’m displaying is based on this embedded schema, the :action for the changeset is set to nil (I think … that’s how I understand it anyway). As such, form_for_errors/1 ignores the errors that are on the nested embedded schemas.

Why isn’t form_for_errors/1 written like this?

defp form_for_errors(%{action: :ignore}), do: []
defp form_for_errors(%{errors: errors}), do: errors

This implementation breaks five different tests, so it’s clear that we mean to ignore the errors when %{action: nil}.

Why is this? Is there some other way that I should approach the problem?

Where Next?

Popular in Questions Top

Darmani72
If I have a post route which an argument: post /my_post_route/:my_param1, MyController.my_post_handler How would get the post params ...
New
fireproofsocks
Forgive me if this is obvious, but how does one delete a database record WITHOUT selecting it first? Ecto.Repo — Ecto v3.14.0 has exampl...
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
jerry
Good day to you all. I have been struggling to get a query involving like and ilike to work. Can anyone assist me on this, please? pro...
New
siddhant3030
Hi, I have to write a raw query for one of my project. But till now I have used ecto queries and don’t have much experience writing raw ...
New
svb
Hi! Currently I want to submit a form by pressing the Enter key. However, since my input field is of type “textarea” this is just adds a...
New
Harrisonl
We have an ECS cluster with 4 services, where each task joins a single cluster, via discovery ECS discovery service. Currently when I de...
New

Other popular topics Top

rms.mrcs
Hi, I need to transform a list of numbers into a map where the keys are the indexes and the values are the original values of the list. ...
New
jononomo
I am trying to figure out how Mix knows whether the environment is test, dev, or prod – where is this set? Thanks.
New
openscript
Hello! Sorry for this astonishing simple question, but I’m really stuck. I try to set up the intellij-elixir plugin, but I don’t know ho...
New
Darmani72
If I have a post route which an argument: post /my_post_route/:my_param1, MyController.my_post_handler How would get the post params ...
New
siddhant3030
Hi, I have to write a raw query for one of my project. But till now I have used ecto queries and don’t have much experience writing raw ...
New
sergio
Kind of like when jquery came out, it was super necessary. Existing drag and drop libraries have a bunch of baggage to support old browse...
New

We're in Beta

About us Mission Statement