arcanemachine

arcanemachine

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
    ]} />

Marked As Solved

SophieDeBenedetto

SophieDeBenedetto

Author of Programming Phoenix LiveView

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.

Where Next?

Popular Pragmatic Bookshelf topics Top

jeffmcompsci
Title: Design and Build Great Web APIs - typo “https://company-atk.herokuapp.com/2258ie4t68jv” (page 19, third bullet in URL list) Typo:...
New
edruder
I thought that there might be interest in using the book with Rails 6.1 and Ruby 2.7.2. I’ll note what I needed to do differently here. ...
New
AleksandrKudashkin
On the page xv there is an instruction to run bin/setup from the main folder. I downloaded the source code today (12/03/21) and can’t see...
New
curtosis
Running mix deps.get in the sensor_hub directory fails with the following error: ** (Mix) No SSH public keys found in ~/.ssh. An ssh aut...
New
hazardco
On page 78 the following code appears: &lt;%= link_to ‘Destroy’, product, class: ‘hover:underline’, method: :delete, data: { confirm...
New
kolossal
Hi, I need some help, I’m new to rust and was learning through your book. but I got stuck at the last stage of distribution. Whenever I t...
New
jwandekoken
Book: Programming Phoenix LiveView, page 142 (157/378), file lib/pento_web/live/product_live/form_component.ex, in the function below: d...
New
EdBorn
Title: Agile Web Development with Rails 7: (page 70) I am running windows 11 pro with rails 7.0.3 and ruby 3.1.2p20 (2022-04-12 revision...
New
davetron5000
Hello faithful readers! If you have tried to follow along in the book, you are asked to start up the dev environment via dx/build and ar...
New
dachristenson
I’ve got to the end of Ch. 11, and the app runs, with all tabs displaying what they should – at first. After switching around between St...
New

Other popular topics Top

Devtalk
Hello Devtalk World! Please let us know a little about who you are and where you’re from :nerd_face:
New
Exadra37
Please tell us what is your preferred monitor setup for programming(not gaming) and why you have chosen it. Does your monitor have eye p...
New
Rainer
My first contact with Erlang was about 2 years ago when I used RabbitMQ, which is written in Erlang, for my job. This made me curious and...
New
PragmaticBookshelf
Rust is an exciting new programming language combining the power of C with memory safety, fearless concurrency, and productivity boosters...
New
New
PragmaticBookshelf
Create efficient, elegant software tests in pytest, Python's most powerful testing framework. Brian Okken @brianokken Edited by Kat...
New
PragmaticBookshelf
Build efficient applications that exploit the unique benefits of a pure functional language, learning from an engineer who uses Haskell t...
New
DevotionGeo
I have always used antique keyboards like Cherry MX 1800 or Cherry MX 8100 and almost always have modified the switches in some way, like...
New
AstonJ
This is cool! DEEPSEEK-V3 ON M4 MAC: BLAZING FAST INFERENCE ON APPLE SILICON We just witnessed something incredible: the largest open-s...
New
PragmaticBookshelf
Lint your docs like code: turn any style guide into enforceable rules with Vale and publish clear, consistent content every time. ...
New

Sub Categories: