fix: handle missing value attr in input component when using name= directly
When calling <.input name="..." ...> without a field or value attr, @value was absent from assigns causing a KeyError. Added default: nil to the attr and use __given__ to detect whether value was explicitly passed so the field clause still reads value from the FormField correctly.
This commit is contained in:
parent
9d232b8368
commit
7668518114
1 changed files with 4 additions and 2 deletions
|
|
@ -258,7 +258,7 @@ defmodule ToweropsWeb.CoreComponents do
|
|||
attr :id, :any, default: nil
|
||||
attr :name, :any
|
||||
attr :label, :string, default: nil
|
||||
attr :value, :any
|
||||
attr :value, :any, default: nil
|
||||
|
||||
attr :type, :string,
|
||||
default: "text",
|
||||
|
|
@ -281,11 +281,13 @@ defmodule ToweropsWeb.CoreComponents do
|
|||
def input(%{field: %FormField{} = field} = assigns) do
|
||||
errors = if Phoenix.Component.used_input?(field), do: field.errors, else: []
|
||||
|
||||
value = if Map.has_key?(assigns.__given__, :value), do: assigns.value, else: field.value
|
||||
|
||||
assigns
|
||||
|> assign(field: nil, id: assigns.id || field.id)
|
||||
|> assign(:errors, Enum.map(errors, &translate_error(&1)))
|
||||
|> assign_new(:name, fn -> if assigns.multiple, do: field.name <> "[]", else: field.name end)
|
||||
|> assign_new(:value, fn -> field.value end)
|
||||
|> assign(:value, value)
|
||||
|> input()
|
||||
end
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue