Design/Performance Question - validate with every change or only on save?

I have a complex schema that is using multiple components to collect parameters. There are about 45 fields. Validating every time the user enters a one-key change seems like a LOT of round trips to the server. My other option is to collect all of the params and then “validate” on Save. The user can then go back and do corrections.

What do you experienced Elixir/LiveView programmers do? Do you always validate on phx-change? Or do you sometimes only validate on phx-submit in order to improve performance?

Performance testing would be helpful to determine if it’s actually an issue and, if so, how much of one. The potential solution can be somewhere in between validating with every change and validating only on save thanks to LiveView’s built-in bindings for rate limiting events on the client side via phx-debounce and phx-throttle.

<form phx-change="validate" phx-submit="save">
  <input type="text" name="user[email]" phx-debounce="blur"/>
  <input type="text" name="user[username]" phx-debounce="2000"/>
</form>

source: Rate limiting events with Debounce and Throttle | Phoenix LiveView

2 Likes

Ahhhhh. Clever! I have not tried those. I had seen phx-debounce but hadn’t fully understood why it was being injected into the . I’ll give that a try. Thanks!

Agreed! Instead of it being more of a binary decision, LiveView gives us the tools to modulate/dial in the desired balance/tradeoff between reactivity and performance.

1 Like