amos-kibet

amos-kibet

Ecto changeset validations cause lag in displaying nested form fields

I have a form that I use to add users to an organization, that looks like this:

The problem:
Clicking on the Add User button takes a few milliseconds (~500ms) before the input fields are displayed, and the delay compounds as I add more users. Note that clicking that button sends a validate event to the server.
Checking the server logs, validation happens for all of the already filled fields. I’m not sure if that’s expected, or I’m doing something wrong in the code. It feels redundant to re-validate all the fields; I expect validation to happen only for the field in focus, but maybe it’s how inputs_for/1 works.

This is my changeset function:

 def invite_changeset(organization, attrs) do
    organization
    |> cast(attrs, [])
    |> cast_assoc(:users,
      with: &User.invitation_changeset/2,
      sort_param: :users_sort,
      drop_param: :users_drop
    )
  end

What I have tried so far:
I am preloading :users that cast_assoc/3 is using. I have tried not running validations untill the form is submitted, but that doesn’t work with inputs_for/1; the form is not being rendered when I click on Add User button on the UI. Also tried providing a changeset that has no validations (just casting) when validating, but the delay is still there, even though cast_assoc/3 does no validation, just casting the params.

The question:
Honestly, I am not entirely sure what the cause of the delay is, but I suspect it is the changeset validation. How can I fix this delay?

Marked As Solved

amos-kibet

amos-kibet

Turns out the delay was being caused by a password hashing step, done by Bcrypt. Skipping it during form validation, and only running it during form submission solved the issue.

Also Liked

LostKobrakai

LostKobrakai

That’s how changesets work. They compare the base state with the complete set of changes in the form. There is no partial validation as changesets do not support any partial delivery of changes nor revalidation (as in removing errors) of already validated fields.

Additionally inputs_for is not granularly change tracked like individual root level inputs. This is a known limitation of how inputs_for is implemented.

Where Next?

Popular in Questions Top

aadeshere1
I have a another noob question about loop. Since elixir is immutable, while loop is not directly possible. total = 10 while total != 0 ...
New
Emily
I have VueJS GUIs with the project generated using Webpack. I have Elixir modules that will need to be used by the VueJS GUIs. I forese...
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
mgjohns61585
Could someone help me? I’m making my first elixir program, number guessing game. I can’t figure out how to convert the user’s guess from ...
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
aalberti333
As the title describes, I’m trying to run Enum.map() over a list of key/value pairs, where the value is a map. My data looks like this: ...
New
lucidguppy
I have a super simple question about elixir - how would I take a file like this foo bar baz and output a new file that enumerates th...
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
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
vonH
In asking this question I am more interested about the expressiveness of the language itself and less concerned about the availability of...
New

Other popular topics Top

vertexbuffer
Hello, can anybody help here..? I have a list of players and I what to delete an element, but every for loop the list is reverting to ori...
New
hariharasudhan94
lets say i have a sample like a = 20; b = 10; if (a > b) do {:ok, "a"} end if (a < b) do {:ok, b} end if (a == b) do {:ok, "equa...
New
danschultzer
None of the current solutions worked well for me, so I went ahead and built a user management system from scratch. This project took far...
548 29703 241
New
mcarvalho
What is the difference between System.get_env and Application.get_env? For example, what are best practices to use one versus another.
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
ovidiubadita
Hey all, I discovered Elixir and I love it. I always wanted to learn a functional programming and I intended to go for Haskell, but afte...
New
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
romenigld
I am trying to run a deploy with docker and I successfully runned with this command: docker build -t romenigld/blog-prod . but when I t...
New
komlanvi
Hi everyone, I was playing with phoenix liveView but I run into an issue. I have a form and want to validate each input text when the te...
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

We're in Beta

About us Mission Statement