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

rms.mrcs
Hi, I need to transform a list of numbers into a map where the keys are the indexes and the values are the original values of the list. ...
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
jononomo
For some reason my phoenix channels are working for me in my local dev environment, but as soon as I deploy via Docker, I get a 403 error...
New
lastday4you
I wanted to check elixir version in phoenix because i found that my elixir is 1.5 but when i use Enum.chunk_by it said the function is un...
New
SoCreat
i’m a new one to elixir which editor can i use vs code? or atom? Thanks! :smiley:
New
dblack
I’ve got an issue with an app and I’ve no idea of how to troubleshoot it. I’m hoping someone here might have seen something similar. I p...
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

Other popular topics Top

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
jononomo
I am trying to figure out how Mix knows whether the environment is test, dev, or prod – where is this set? Thanks.
New
lanycrost
Hi everyone! I need implement if…else if…else condition from my elixir code, and anymore of this control flow structures not work proper...
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
WestKeys
Currently suffering from paralysis by [HTTP client] analysis. This is rather unusual in Elixirland as there tends to be consensus on the ...
New
jason.o
In the code below, if the create action is not set to accept “extra_key” as an input, it errors out with a message shown above. Is there ...
New