How to set to_form error with a nested property

Hey folks,

I’m trying to use liveview without Ecto, how can I use a form with a nested property in to_form and how to set errors

I’m trying to do this:

to_form(%{"address" => %{ "line_1" => "" }}, errors: [address: [line_1: :too_short]])

and on the html.heex


<.inputs_for :let={address} for={@form[:address]}>
  <.input
    field={address[:line_1]}
    label="line 1"
    type="text"
  />
</.inputs_for>

The error is never set on form

errors is an attr on the input component, so you can supply it manually.

The default map based implementation of Phoenix.HTML.FormData only works well for simple, non-nested cases.

4 Likes