fix(forms): keep touched field state dialyzer-safe
This commit is contained in:
parent
ac354f8073
commit
e7286f5608
|
|
@ -10,10 +10,11 @@ defmodule WhoNeedHelpWeb.FormParams do
|
|||
|> Enum.reject(fn key ->
|
||||
Map.has_key?(data, @live_view_unused_prefix <> to_string(key))
|
||||
end)
|
||||
|> MapSet.new(&to_string/1)
|
||||
|> Enum.map(&to_string/1)
|
||||
|> Enum.uniq()
|
||||
end
|
||||
|
||||
def touched_structured_keys(_params), do: MapSet.new()
|
||||
def touched_structured_keys(_params), do: []
|
||||
|
||||
def drop_unused_structured_fields(%{"structured_data" => data} = params)
|
||||
when is_map(data) do
|
||||
|
|
@ -36,7 +37,7 @@ defmodule WhoNeedHelpWeb.FormParams do
|
|||
field ->
|
||||
key = to_string(field["key"])
|
||||
|
||||
if submitted? or MapSet.member?(touched_keys, key) do
|
||||
if submitted? or key in touched_keys do
|
||||
[{error_kind(message, key), field_label(field, locale)}]
|
||||
else
|
||||
[]
|
||||
|
|
|
|||
|
|
@ -13,7 +13,7 @@ defmodule WhoNeedHelpWeb.ActivityLive.New do
|
|||
|> assign(:categories, Catalog.list_categories(:activity))
|
||||
|> assign(:selected_category, nil)
|
||||
|> assign(:structured_fields, [])
|
||||
|> assign(:structured_touched_keys, MapSet.new())
|
||||
|> assign(:structured_touched_keys, [])
|
||||
|> assign(:structured_submit_attempted, false)
|
||||
|> assign(:form, to_form(Activities.change_activity(%Activity{})))}
|
||||
end
|
||||
|
|
@ -21,8 +21,8 @@ defmodule WhoNeedHelpWeb.ActivityLive.New do
|
|||
@impl true
|
||||
def handle_event("validate", %{"activity" => params}, socket) do
|
||||
touched_keys =
|
||||
MapSet.union(
|
||||
socket.assigns.structured_touched_keys,
|
||||
Enum.uniq(
|
||||
socket.assigns.structured_touched_keys ++
|
||||
FormParams.touched_structured_keys(params)
|
||||
)
|
||||
|
||||
|
|
|
|||
|
|
@ -27,7 +27,7 @@ defmodule WhoNeedHelpWeb.RequestLive.New do
|
|||
|> assign(:categories, Catalog.list_categories())
|
||||
|> assign(:selected_category, nil)
|
||||
|> assign(:structured_fields, [])
|
||||
|> assign(:structured_touched_keys, MapSet.new())
|
||||
|> assign(:structured_touched_keys, [])
|
||||
|> assign(:structured_submit_attempted, false)
|
||||
|> assign(:submission_errors, [])
|
||||
|> assign(:form, to_form(changeset))}
|
||||
|
|
@ -36,8 +36,8 @@ defmodule WhoNeedHelpWeb.RequestLive.New do
|
|||
@impl true
|
||||
def handle_event("validate", %{"help_request" => params}, socket) do
|
||||
touched_keys =
|
||||
MapSet.union(
|
||||
socket.assigns.structured_touched_keys,
|
||||
Enum.uniq(
|
||||
socket.assigns.structured_touched_keys ++
|
||||
FormParams.touched_structured_keys(params)
|
||||
)
|
||||
|
||||
|
|
|
|||
Loading…
Reference in New Issue
Block a user