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

senggen
Erlang/OTP 25 [erts-13.2.2] [source] [64-bit] [smp:8:8] [ds:8:8:10] [async-threads:1] 15:22:35.803 [error] gen_event {lager_file_backend...
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
ycv005
I have followed this StackOverflow post to install the specific version of Erlang. And When I am running mix ecto.setup then getting fol...
New
Werner
Hi, I’m using Ubuntu 18.04 and after updating to OTP-24.0 yesterday i have this warning when I run “mix local.hex”: 14:57:30.512 [warn] ...
New
polypush135
As many of you may have realized by now (sorry for all the posts here) I’ve been working on a db problem where I’m trying to aggregate a ...
New
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
script
If I have a string “1000 cfu/ml” . I want to remove the characters and / and space . So the string is like this "1000" What is the ...
New
vac
Hi, I'm quite new in Elixir and I'm trying to format a string to a PEM format. I have the certificate value like MIIDBTCCAe2...... and ...
New
stefanluptak
Hello everybody, usually, I use a 29" ultra-wide monitor for VSCode which can easily accomodate explorer (files panel) + file with code ...
New
9mm
I am constructing a JSON object (map) and I need to conditionally set a field. I’m trying to write proper elixir-way code… and I’m at a l...
New

Other popular topics Top

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
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
sergio_101
I am VERY much an elixir newbie. I have taken one elixir course and one phoenix course on Udemy. During that course, I saw the instructor...
New
KronicDeth
Elixir plugin for JetBrain’s IntelliJ Platform (including Rubymine) This is a plugin that adds support for Elixir to JetBrains IntelliJ...
289 35421 110
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
chensan
I have a User schema with a :from_id field set to type :string: defmodule TweetBot.Repo.Migrations.CreateUsers do use Ecto.Migration ...
New
ashish173
I am using Ecto timestamps with postgres, I can see the timestamps() use the :naive_dateime but for my use case I wanted to store the ti...
New
electic
Hi, I am new to Elixir. I am trying to use the DateTime component to insert a date into MySQL however the there seems to be no way to fo...
New
beno
I will often find my self writing things similar to: case some_value do nil -> something() "" -> something() _ -> someth...
New
magnetic
Hey :wave:t3: Elixir community, I’ve been learning Elixir, and working on some side projects. My editor of choice is VSCode, and althoug...
New

We're in Beta

About us Mission Statement