Skip to content

Commit 19680de

Browse files
committed
Preserve original name when as is not provided to simple_form
Remove the default `nil` value for the `as` attribute in the generated `simple_form` core component. This default interferes with the form name present in the form struct provided through the component's `for` attribute when used with `:let=`. For example, consider the following code: ``` <.simple_form :let={f} for={@Form}> <%= inspect(f.name) %> <.input field={f[:bar]} label="Bar" /> </.simple_form> ``` When rendered, `f.name` is `nil`, and the `name` attribute of the form field only contains `bar`, missing the proper field name prefix from the form struct: ``` <div class="mt-10 space-y-8 bg-white"> nil <div data-phx-id="m9-phx-GAgAfRg6YU4XV6tC"> <label for="bar" class="block text-sm font-semibold leading-6 text-zinc-800" data-phx-id="m10-phx-GAgAfRg6YU4XV6tC"> Bar </label> <input type="text" name="bar" id="bar" class="mt-2 block w-full rounded-lg text-zinc-900 focus:ring-0 sm:text-sm sm:leading-6 border-zinc-300 focus:border-zinc-400"> </div> </div> ``` This issue been reported [over here](#5901). Fixes #5901
1 parent dad4527 commit 19680de

File tree

2 files changed

+8
-4
lines changed

2 files changed

+8
-4
lines changed

installer/templates/phx_web/components/core_components.ex

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@ defmodule <%= @web_namespace %>.CoreComponents do
120120
</.simple_form>
121121
"""
122122
attr :for, :any, required: true, doc: "the data structure for the form"
123-
attr :as, :any, default: nil, doc: "the server side parameter to collect all input under"
123+
attr :as, :any, doc: "the server side parameter to collect all input under"
124124

125125
attr :rest, :global,
126126
include: ~w(autocomplete name rel action enctype method novalidate target multipart),
@@ -130,8 +130,10 @@ defmodule <%= @web_namespace %>.CoreComponents do
130130
slot :actions, doc: "the slot for form actions, such as a submit button"
131131

132132
def simple_form(assigns) do
133+
assigns = assign(assigns, :as, if(assigns[:as], do: %{as: assigns[:as]}, else: %{}))
134+
133135
~H"""
134-
<.form :let={f} for={@for} as={@as} {@rest}>
136+
<.form :let={f} for={@for} {@as} {@rest}>
135137
<div class="mt-10 space-y-8 bg-white">
136138
<%%= render_slot(@inner_block, f) %>
137139
<div :for={action <- @actions} class="mt-2 flex items-center justify-between gap-6">

priv/templates/phx.gen.live/core_components.ex

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@ defmodule <%= @web_namespace %>.CoreComponents do
120120
</.simple_form>
121121
"""
122122
attr :for, :any, required: true, doc: "the data structure for the form"
123-
attr :as, :any, default: nil, doc: "the server side parameter to collect all input under"
123+
attr :as, :any, doc: "the server side parameter to collect all input under"
124124

125125
attr :rest, :global,
126126
include: ~w(autocomplete name rel action enctype method novalidate target multipart),
@@ -130,8 +130,10 @@ defmodule <%= @web_namespace %>.CoreComponents do
130130
slot :actions, doc: "the slot for form actions, such as a submit button"
131131

132132
def simple_form(assigns) do
133+
assigns = assign(assigns, :as, if(assigns[:as], do: %{as: assigns[:as]}, else: %{}))
134+
133135
~H"""
134-
<.form :let={f} for={@for} as={@as} {@rest}>
136+
<.form :let={f} for={@for} {@as} {@rest}>
135137
<div class="mt-10 space-y-8 bg-white">
136138
<%%= render_slot(@inner_block, f) %>
137139
<div :for={action <- @actions} class="mt-2 flex items-center justify-between gap-6">

0 commit comments

Comments
 (0)