mwmiller

mwmiller

Form validation patterns in Hologram

I tried this out and it works great! Except for that it exposes a problem I had before with the $blur event.

My site has a form which includes user-enterable numeric strings. These are sent to the server for validation and then new data is presented. I don’t have a submit button or anything, it’s just “magic.”

I initially tried to use $blur events to avoid sending on each keystroke, waiting for the user input to appear complete. However, I couldn’t figure out how to get the value of the input therewith. So I moved to $change and chalked it up to my ignorance.

With the above version, I now get crazy responsive stuff with each keystroke… but this makes it impossible to enter a long number because it’s already validated, returned and changed in the very box in which I am typing.

I’ve learned a lot since I first started this (thanks!), so I tried $blur again with high hopes. Not so much. I put in a debug command called from the $blur-fired action:

Debug command received: %{event: %{}}

So, I know a particular input has blurred and I should check its value, but I don’t get this value in the event. Should I be doing something in the action to extract the input value from the component?

Most Liked

bartblast

bartblast

Creator of Hologram

@mwmiller That part about “crazy responsive stuff with each keystroke” honestly sounds like it could be a Hologram advertisement! :rofl: But I understand the UX concern you’re raising.

Here are the key technical facts about Hologram’s event system:

Synthetic Events: Hologram uses synthetic events (similar to React). For example, $change maps to the native input event, not the native change event.

Form-level vs Field-level Events: The $change event behaves differently depending on where you put it:

  • On a text input: triggers on every keystroke (maps to input event)
  • On a form: behaves like the native change event - typically triggers when a field loses focus

$blur Events: These map directly to native blur events and can be triggered on many element types (links, buttons, etc.), not just form elements. That’s why they don’t include a value property - it wouldn’t make sense for non-form elements.

Client-side Validation: Here’s the key architectural point - you don’t need to go through the server for initial validation! Hologram runs Elixir in the browser, so you can do validation client-side. Your validation code will be isomorphic - the same Elixir validation logic (including Ecto changesets) can run both client-side and server-side. You should still validate server-side as a security measure and for data integrity (e.g., when saving to database, checking business rules, ensuring referential integrity), but the initial user feedback can be immediate and local.

Suggested Pattern: For your use case, try this approach:

  1. Use $change event on your input field to sync the value with your component state instantly only if you need to use that value elsewhere in your component or modify it through other actions - otherwise, you can skip this step since the form’s $change event will include all current form field data
  2. Use $change event on the form element to trigger validation when the user stops inputting (e.g., when the field loses focus)
  3. Run your validation logic client-side for immediate feedback
  4. Validate again server-side when actually persisting the data
garrison

garrison

There is a very particular “genre” of mistake you’re probably making here, but instead of trying to explain it I will just link this article which goes over it in enormous detail:

This is somewhat off-topic (as I alluded to earlier in the thread, this is an interface design problem), but maybe you will find it helpful.

mwmiller

mwmiller

Just a minor update, to sort of close the loop.

I am now using Hologram 0.6.2. Today I implemented an intent system and it’s working well. I do still have some kinks to work out based on my particular domain. In any case, I wanted to thank everyone in the thread here for their time and guidance.

Where Next?

Popular in Questions Top

RisingFromAshes
I’ve read in another post that it may be possible with a router helper - but I couldn’t find an appropriate one, and tbh, I’m still just ...
New
jononomo
I am trying to figure out how Mix knows whether the environment is test, dev, or prod – where is this set? Thanks.
New
skosch
To my knowledge, put_in, Map.update etc. all have the one limitation of not automatically creating intermediate keys when needed (for exa...
New
Fl4m3Ph03n1x
About me? ( if you have nothing better to do than reading about some random guy in the internet :stuck_out_tongue: ) Hello all, this is ...
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
pmjoe
I have a relationship of love and hate with Elixir. Lots of things are just absolutely right, but there are some things that are kind of ...
New
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

Other popular topics Top

JeremM34
Hello, how can I check the Phoenix version ? Thanks !
New
joaquinalcerro
Hi there, I am working with Ecto-Postgresql and I need to call all of the records from a specific table but the table has 40,000 records...
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
nsuchy
Hi. I’ve noticed that Windows Powershell has it’s own IEX command and you cannot access Elixir’s IEX due to the conflict. This isn’t a cr...
New
alice
Hey, Just curious what are the main benefits of Elixir compared to Clojure? When is Elixir more useful than Clojure and vice versa? Th...
New
SoCreat
i’m a new one to elixir which editor can i use vs code? or atom? Thanks! :smiley:
New

We're in Beta

About us Mission Statement