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

vegabook
I’m brand new to Phoenix and I have stripped one of the demo applications to the bone. I just want to get an svg up on the screen. Here i...
New
minhajuddin
I have seen a lot of code which picks the first element from a list using Enum.at(0) instead of List.first. Is there a reason why people ...
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
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
jay1
Why is it that the mnesia database isn’t the most preferred database for use in Elixir/Phoenix?
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
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

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
vonH
In asking this question I am more interested about the expressiveness of the language itself and less concerned about the availability of...
New
hariharasudhan94
Lets say I have map like this fetching from my database %{"_id" =&gt; #BSON.ObjectId&lt;58eb1a7a9ad169198c3dXXXX&gt;, "email" =&gt; ...
New
axelson
This post is a wiki (feel free to hit the edit button near the bottom right of this post to add your own changes!) This post collects co...
239 49084 226
New
msaraiva
Surface is an experimental library built on top of Phoenix LiveView and its new LiveComponent API that aims to provide a more declarative...
564 44139 214
New
Harrisonl
We have an ECS cluster with 4 services, where each task joins a single cluster, via discovery ECS discovery service. Currently when I de...
New

We're in Beta

About us Mission Statement