improvement: add support for target and only_touched? validate opts

chore: update elixir/erlang versions
This commit is contained in:
Zach Daniel 2023-07-19 00:35:29 -04:00
parent 900c5916a2
commit 22611cd4e1
3 changed files with 41 additions and 4 deletions

View file

@ -1,2 +1,2 @@
erlang 25.2.2
elixir 1.14.3
erlang 26.0.2
elixir 1.15.4

View file

@ -856,6 +856,15 @@ defmodule AshPhoenix.Form do
type: :boolean,
default: true,
doc: "Set to false to hide errors after validation"
],
target: [
type: {:list, :string},
doc: "The `_target` param provided by phoenix. Used to support the `only_touched?` option."
],
only_touched?: [
type: :boolean,
default: false,
doc: "If set to true, only fields that have been marked as touched will be used"
]
]
@ -876,7 +885,32 @@ defmodule AshPhoenix.Form do
|> Phoenix.HTML.FormData.to_form(form.options)
end
def validate(form, new_params, opts) do
def validate(%{name: name} = form, new_params, opts) do
form =
case opts[:target] do
[^name | target] ->
field = List.last(target)
path = :lists.droplast(target)
case path do
[] ->
touch(form, field)
path ->
update_form(form, path, &touch(&1, field))
end
_ ->
form
end
new_params =
if opts[:only_touched?] do
Map.take(new_params, form.touched_forms)
else
new_params
end
opts = validate_opts_with_extra_keys(opts, @validate_opts)
prepare_source = form.prepare_source || (& &1)

View file

@ -193,7 +193,10 @@ defmodule AshPhoenix.FormTest do
test "blank form values unset - helps support dead view forms" do
form =
Form.for_create(PostWithDefault, :create, api: Api, exclude_fields_if_empty: [:text, :title])
Form.for_create(PostWithDefault, :create,
api: Api,
exclude_fields_if_empty: [:text, :title]
)
{:ok, post} = Form.submit(form, params: %{"title" => "", "text" => "bar"})
assert post.text == "bar"