marick

marick

`inputs_for(... prepend: ...)` doesn't process the prepended value in this case. I think

A call to inputs_for that uses :prepend seems to skip the prepended value when the containing form has errors (that is: was created from a changeset with errors). Is this a bug?


Here’s what a form looks like for an animal with no “service gaps”:

This form represents an animal that has_many service gaps. In this case, it has none.

The code that produced the form’s HTML looks like this:

form_for @changeset, "/not-used", fn f ->
   ... 
   inputs_for(f, :service_gaps, [prepend: [%ServiceGap{}]], fn gap_f -> ...

The form itself looks like this:

%Phoenix.HTML.Form{
  data: %Crit.Usables.Schemas.Animal{...
    service_gaps: [],...
  },
  errors: [],
}

All is OK in this case.

Now suppose I submit the form with an error in one of the animal fields. In this case, I try to rename it to an already-existing name. The resulting changeset gets fed to form_for and the form looks like this:

%Phoenix.HTML.Form{
  data: %Crit.Usables.Schemas.Animal{...
    service_gaps: [],...
  },
  errors: [
    name: {"has already been taken",
     [constraint: :unique, constraint_name: "unique_available_names"]}
  ],
}

In this case, the function argument to inputs_for is not called for the :prepend value. Since there are no actual service gaps, the function is never called. That means:

  1. If a user chooses to edit an animal, she can add a service gap and change properties of the animal.
  2. But, if she makes a mistake changing an animal’s properties, the ability to add a new service gap mysteriously disappears.

In the case where the animal already has service gaps, the non-prepend values for inputs_for are still processed. For example, here’s the initial form for an animal with a single service gap:

The same mistake (picking a new name the same as an existing name) produces this form:

(The “Add a gap” heading is wrong because the code assumes the first set of fields will be for the prepended empty Animal. But it’s not, because the empty Animal got skipped.)


So: it seems that, in the presence of an error in the top-level Form structure, inputs_for ignores specifically the prepended value used to create a new “assoc” value. But it processes other values.

First Post!

marick

marick

I think I figured it out. Recap: the animal is submitted with N ServiceGap forms that might have been changed. They are for existing structs. It’s also submitted with a new (prepended) ServiceGap form that might be empty (no attempt to fill it in) or with some values. In the latter case, it’s to be validated and possibly inserted.

There is an error somewhere in at least one of the form values.

  1. If there was no change made to the prepended form, it should probably re-appear (still empty) in the error-annotated form displayed to the user.
  2. If there was an attempt to create a new ServiceGap and that new form contains errors, the form should be displayed with the errors marked for correction. There probably shouldn’t be another empty form.
  3. If the new form was filled in and it’s fine, it should again be displayed. Again, there probably shouldn’t be another new form.

The inputs_for behavior supports the latter two cases. It’s awkward for the first one because an empty form vanishes because of a mistake made someone else. I think this case just has to be handled specially.

I’m inclined not to use prepend and instead put the logic of when an empty form should appear down around the changeset. It’s business logic, of a sort, I guess.

Where Next?

Popular in Questions Top

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
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
greenz1
I have a phoenix application from which a user can download multiple(5-6) files of size 1MB. I couldn’t find anything related to sending ...
New
New
freewebwithme
Using vs code and installed ElixirLS: support and debugger. And I got an error popped up on start up says Failed to run ‘elixir’ comma...
New
vegabook
I'm brand new to Phoenix and I have stripped one of the demo applications to the bone. I just want to get an svg up on the screen. Here i...
New
baxterw3b
Hi guys, i’m new in the Elixir world, and i have to say, that i love it! i’m having some problem to understand anonymous functions with ...
New
jason.o
In the code below, if the create action is not set to accept “extra_key” as an input, it errors out with a message shown above. Is there ...
New
srinivasu
How to handle excepions in elixir? Suppose i have A, B, C ,D, E modules. and each module has get() function. A.get() method will call th...
New
shijith.k
I am trying to start a new phoenix project with elixir 1.9, but mix phx.new does not work. It says that ** (Mix) The task "phx.new" could...
New

Other popular topics Top

marius95
Hello everyone, I try to use an Javascript Event Handler in my root.html.leex file. Therefore I created a function in the app.js file: ...
New
gshaw
What is the idiomatic way of matching for not nil in Elixir? E.g., First way: defp halt_if_not_signed_in(conn, signed_in_account) when...
New
shahryarjb
Hello, I have map which I want to convert it to string like this: the map: %{last_name: "tavakkoli", name: "shahryar"} the string I ne...
New
belgoros
I’m not a pro in using Regex and can’t figure out why the following behaviour happens, especially if we take into account the difference ...
New
gausby
I asked this very same question on twitter and got some interesting feedback, but I thought it would be a good question to ask here as we...
1207 39247 209
New
SoCreat
i’m a new one to elixir which editor can i use vs code? or atom? Thanks! :smiley:
New
AstonJ
We’ve put together this wiki for Phoenix LiveView - please feel free to add any info you feel is worth including. What is Phoenix LiveV...
New
jononomo
For some reason my phoenix channels are working for me in my local dev environment, but as soon as I deploy via Docker, I get a 403 error...
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
vonH
In asking this question I am more interested about the expressiveness of the language itself and less concerned about the availability of...
New

We're in Beta

About us Mission Statement