fix(forms): keep touched field state dialyzer-safe

This commit is contained in:
SimpleTest 2026-07-24 11:12:32 +03:00
parent ac354f8073
commit e7286f5608
3 changed files with 12 additions and 11 deletions

View File

@ -10,10 +10,11 @@ defmodule WhoNeedHelpWeb.FormParams do
|> Enum.reject(fn key -> |> Enum.reject(fn key ->
Map.has_key?(data, @live_view_unused_prefix <> to_string(key)) Map.has_key?(data, @live_view_unused_prefix <> to_string(key))
end) end)
|> MapSet.new(&to_string/1) |> Enum.map(&to_string/1)
|> Enum.uniq()
end end
def touched_structured_keys(_params), do: MapSet.new() def touched_structured_keys(_params), do: []
def drop_unused_structured_fields(%{"structured_data" => data} = params) def drop_unused_structured_fields(%{"structured_data" => data} = params)
when is_map(data) do when is_map(data) do
@ -36,7 +37,7 @@ defmodule WhoNeedHelpWeb.FormParams do
field -> field ->
key = to_string(field["key"]) 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)}] [{error_kind(message, key), field_label(field, locale)}]
else else
[] []

View File

@ -13,7 +13,7 @@ defmodule WhoNeedHelpWeb.ActivityLive.New do
|> assign(:categories, Catalog.list_categories(:activity)) |> assign(:categories, Catalog.list_categories(:activity))
|> assign(:selected_category, nil) |> assign(:selected_category, nil)
|> assign(:structured_fields, []) |> assign(:structured_fields, [])
|> assign(:structured_touched_keys, MapSet.new()) |> assign(:structured_touched_keys, [])
|> assign(:structured_submit_attempted, false) |> assign(:structured_submit_attempted, false)
|> assign(:form, to_form(Activities.change_activity(%Activity{})))} |> assign(:form, to_form(Activities.change_activity(%Activity{})))}
end end
@ -21,9 +21,9 @@ defmodule WhoNeedHelpWeb.ActivityLive.New do
@impl true @impl true
def handle_event("validate", %{"activity" => params}, socket) do def handle_event("validate", %{"activity" => params}, socket) do
touched_keys = touched_keys =
MapSet.union( Enum.uniq(
socket.assigns.structured_touched_keys, socket.assigns.structured_touched_keys ++
FormParams.touched_structured_keys(params) FormParams.touched_structured_keys(params)
) )
params = FormParams.drop_unused_structured_fields(params) params = FormParams.drop_unused_structured_fields(params)

View File

@ -27,7 +27,7 @@ defmodule WhoNeedHelpWeb.RequestLive.New do
|> assign(:categories, Catalog.list_categories()) |> assign(:categories, Catalog.list_categories())
|> assign(:selected_category, nil) |> assign(:selected_category, nil)
|> assign(:structured_fields, []) |> assign(:structured_fields, [])
|> assign(:structured_touched_keys, MapSet.new()) |> assign(:structured_touched_keys, [])
|> assign(:structured_submit_attempted, false) |> assign(:structured_submit_attempted, false)
|> assign(:submission_errors, []) |> assign(:submission_errors, [])
|> assign(:form, to_form(changeset))} |> assign(:form, to_form(changeset))}
@ -36,9 +36,9 @@ defmodule WhoNeedHelpWeb.RequestLive.New do
@impl true @impl true
def handle_event("validate", %{"help_request" => params}, socket) do def handle_event("validate", %{"help_request" => params}, socket) do
touched_keys = touched_keys =
MapSet.union( Enum.uniq(
socket.assigns.structured_touched_keys, socket.assigns.structured_touched_keys ++
FormParams.touched_structured_keys(params) FormParams.touched_structured_keys(params)
) )
params = FormParams.drop_unused_structured_fields(params) params = FormParams.drop_unused_structured_fields(params)