Programming Phoenix LiveView B9.0: (Form Episode 3) Form code syntax does not work as written (p. 216/217) (PDF)

This is the same issue as this one (I’ll call it Form Episode 2) and this one (I’ll call it Form Episode 1). Unsurprisingly, it requires the same solution as the other 2 issues.

On page 216/217, the following code produces an error:

    <.input field={{f, :user_id}} type="hidden" />
    <.input field={{f, :product_id}} type="hidden" />
    <.input field={{f, :stars}} type="rating" prompt="Rating"
    options={["★★★★★": 5, "★★★★": 4, "★★★": 3, "★★": 2, "★": 1
    ]} />

The error reads:

key :name not found in: %{
  id: nil,
  label: nil,
  type: "hidden",
  prompt: nil,
  field: {%Phoenix.HTML.Form{
     source: #Ecto.Changeset<
       action: nil,
       changes: %{},
       errors: [stars: {"can't be blank", [validation: :required]}],
       data: #Pento.Survey.Rating<>,
       valid?: false
     >,
     impl: Phoenix.HTML.FormData.Ecto.Changeset,
     id: "rating-form-1",
     name: "rating",
     data: %Pento.Survey.Rating{
       __meta__: #Ecto.Schema.Metadata<:built, "ratings">,
       id: nil,
       stars: nil,
       user_id: 1,
       user: #Ecto.Association.NotLoaded<association :user is not loaded>,
       product_id: 1,
       product: #Ecto.Association.NotLoaded<association :product is not loaded>,
       inserted_at: nil,
       updated_at: nil
     },
     hidden: [],
     params: %{},
     errors: [],
     options: [
       method: "post",
       id: "rating-form-1",
       multipart: false,
       "phx-target": %Phoenix.LiveComponent.CID{cid: 1},
       "phx-submit": "save"
     ],
     index: nil,
     action: nil
   }, :user_id},
  errors: [],
  rest: %{},
  __changed__: nil,
  inner_block: [],
  __given__: %{
    type: "hidden",
    field: {%Phoenix.HTML.Form{
       source: #Ecto.Changeset<
         action: nil,
         changes: %{},
         errors: [stars: {"can't be blank", [validation: :required]}],
         data: #Pento.Survey.Rating<>,
         valid?: false
       >,
       impl: Phoenix.HTML.FormData.Ecto.Changeset,
       id: "rating-form-1",
       name: "rating",
       data: %Pento.Survey.Rating{
         __meta__: #Ecto.Schema.Metadata<:built, "ratings">,
         id: nil,
         stars: nil,
         user_id: 1,
         user: #Ecto.Association.NotLoaded<association :user is not loaded>,
         product_id: 1,
         product: #Ecto.Association.NotLoaded<association :product is not loaded>,
         inserted_at: nil,
         updated_at: nil
       },
       hidden: [],
       params: %{},
       errors: [],
       options: [
         method: "post",
         id: "rating-form-1",
         multipart: false,
         "phx-target": %Phoenix.LiveComponent.CID{cid: 1},
         "phx-submit": "save"
       ],
       index: nil,
       action: nil
     }, :user_id},
    __changed__: nil
  },
  multiple: false
}

To fix the error, change the offending lines to read:

    <.input field={f[:user_id]} type="hidden" />
    <.input field={f[:product_id]} type="hidden" />
    <.input field={f[:stars]} type="rating" prompt="Rating"
    options={["★★★★★": 5, "★★★★": 4, "★★★": 3, "★★": 2, "★": 1
    ]} />

[deleted]

Thanks for your feedback here. This and all other forms are being updated to work with the latest version of LiveView as we speak. We’ll be release a new Beta version of the book in the next few weeks.