phoenix_html icon indicating copy to clipboard operation
phoenix_html copied to clipboard

Form does not display errors for nested embeds while using `:let`

Open rmoorman opened this issue 6 months ago • 2 comments

When using nested ecto embeds in a live view through form and inputs_for, while simultaneously using the :let binding, errors for the nested embed no longer show up in the browser.

I don't know if that is to be expected or if this is a symptom of a problem with the let binding or the form component for example.

I came across this problem while fiddling around a bit with the example code for issue https://github.com/phoenixframework/phoenix/issues/5901.

For demonstration purposes, I created another example repo which can be used to reproduce this specific issue.

The problem

The problem basically comes down to the following: image In the screenshot above, there are 3 embeds visible in the form, all of them have errors in the name fields, but only the first and the third embed show the error. I did the same to all fields, clicked inside and typed another letter to try to trigger the error display.

Below the fields, I printed the form struct for reference where the errors can be seen.

The trigger

The problem is triggered when using the form, inputs_for, and input like this:

    <.form :let={f} for={@form} phx-change="change">
        <.inputs_for :let={nested} field={f[:nested_with_let]}>
          <.input field={nested[:name]} label="Name" />
        </.inputs_for>
    </.form>

The :let is used on the form to bind the form to f, which is then used to provide the field to inputs_for and somewhere there seems to be the culprit.

At first glance, there does not seem to be a problem with inputs_for's behavior. I added a more deeply nested example to the example repo (using the @form assign directly instead of f for the first level), and nesting inputs_for twice, while binding the form struct does seem to work there.

How to reproduce

As mentioned, there is an example repository that can be used to reproduce this problem. There, I bootstrapped a new phoenix application and added the example liveview in hello/lib/hello_web/live/form_live.ex which includes the schema and the template.

rmoorman avatar Aug 17 '24 02:08 rmoorman