marick

marick

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.

Showing Posts 1 to 1

marick

marick OP

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.

— All posts loaded —

Where Next? Top

Trending in Questions Top

katta
I having some trouble figuring out if I have set myself too strict of standards for my production server. Currently I can handle 75% of r...
New
brecabral
Documentation While reading the Scoped Routes section, I noticed that the documentation currently refers to a problem without explainin...
New
achenet
Hello, I’m trying to build a basic Phoenix web-app, and I’d like to use Tailwind. However, when I launch mix phx.server, I get an error...
New
kpanic
Hi everyone, I am toying with the idea of building a “match maker” for giving personal help to people that wants to start coding. I sta...
New
asweet-confluent
I recently noticed that Elixir’s Logger defaults its primary log level to :debug when no :logger, :level application configuration is pre...
New
Cxx-mlr
I’m working on a small exercise involving update_in/3, and I came up with this solution: data = %{ name: "Periodic Table", category:...
New
ChrisAmelia
I’ve got trouble wrapping my head around the order in which functions are called in this snippet (from Phoenix’s authentication): toke...
New

Other Trending Topics Top

GenericJam
Edit: 2026 May 15 - This post is archived. Mob is alive!! Main docs: mob v0.7.11 — Documentation A bit of explanation for the slightly c...
New
JesseHerrick
Hey, I’m Jesse and I’m the main contributor behind Dexter, a full-featured, lightning-fast Elixir LSP optimized for large codebases. It s...
New
mudasobwa
I am happy to introduce the very α version of the new programming language compiled to BEAM. Welcome Cure. It has literally three kille...
New
garrison
Hobbes is a low-level distributed database for the Elixir programming language. Hobbes provides a simple, safe, and scalable storage lay...
New
mhanberg
Hi everyone! The first release candidate for the Expert language server project is now available! We’ve published a press release detai...
New
budgie
A little off-topic, but I feel like people here have a good head on their shoulders. I used to be quite good at making software. Was luc...
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews