boiserunner

boiserunner

Errors in Liveview: Parse error ... expected attribute name

I’m stumped. I have a LiveView form tied to a Changeset (rather than a schema) like so:

~H"""
  <.form as={:form} for={@changeset} :let={f} phx-change="validate" phx-submit="save">
    <.input field={f[:email]} />
  </.form>
"""

I can see errors collecting into my changeset on validate. What’s the best pattern for showing an error from the changeset? I see reference to phx-feedback-for and an <.error> helper, but something like this:

<div phx-feedback-for={:email}>
  <.input field={f[:email]} />
  <.error :for={msg <- @changeset.errors}><%%= msg %></.error>
</div>

leaves me with an Parse error ... expected attribute name.

Thank you in advance.

Most Liked

boiserunner

boiserunner

An update in case it’s helpful. I can certainly pattern match like:

<.error :for={{:email, {error_msg, _}} <- @changeset.errors}>
  <%= error_msg %>
</.error>

However, I suspect I’m still doing something wrong. The docs for phx-feedback-for seem to indicate that wrapping my error with a field value like :email would filter errors just for that field. But I have to explicitly match on :email to avoid multiple error messages appearing in one spot. It just feels a bit cumbersome or redundant, but it works.

codeanpeace

codeanpeace

That’s because the input component from the generated core components uses the field scoped @errors rather than the entire form’s @changeset.errors. It then uses these input field specific errors to render error components within the input component itself.

  def input(%{field: %Phoenix.HTML.FormField{} = field} = assigns) do
    assigns
    |> assign(field: nil, id: assigns.id || field.id)
    |> assign(:errors, Enum.map(field.errors, &translate_error(&1)))
    ...
  end

Last Post!

boiserunner

boiserunner

That makes sense. Now I can see where @errors is coming from — assigns(:errors ...) in the core component. Thank you.

Where Next?

Popular in Questions Top

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
joeerl
Hello again - after a longish gap I’ve decided I really must dig into Elixir and see what’s been happening here - so I have a few questio...
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
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
WestKeys
Currently suffering from paralysis by [HTTP client] analysis. This is rather unusual in Elixirland as there tends to be consensus on the ...
New
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

Other popular topics Top

grych
Hi folks, Few months ago I have announced the proof-of-concept of the library to manipulate the browsers DOM objects directly from Elixi...
639 54006 488
New
JakeBecker
TL;DR: I’ve just released an implementation of Microsoft’s IDE-independent Language Server Protocol for Elixir. It adds language support ...
1144 54921 245
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
AngeloChecked
What learn first? Rust or Elixir Hi Elixir community! I’m here because i want learn a new language. I’m a junior developer and mainly i ...
New
SoCreat
i’m a new one to elixir which editor can i use vs code? or atom? Thanks! :smiley:
New
JorisKok
I have a server on AWS, and was running a load test using artillery. When looking at the Phoenix dashboard I see the Ports going to 100% ...
New

We're in Beta

About us Mission Statement