mirror of
https://github.com/ash-project/ash_phoenix.git
synced 2024-09-19 06:42:47 +12:00
improvement: add support for target
and only_touched?
validate opts
chore: update elixir/erlang versions
This commit is contained in:
parent
900c5916a2
commit
22611cd4e1
3 changed files with 41 additions and 4 deletions
|
@ -1,2 +1,2 @@
|
||||||
erlang 25.2.2
|
erlang 26.0.2
|
||||||
elixir 1.14.3
|
elixir 1.15.4
|
||||||
|
|
|
@ -856,6 +856,15 @@ defmodule AshPhoenix.Form do
|
||||||
type: :boolean,
|
type: :boolean,
|
||||||
default: true,
|
default: true,
|
||||||
doc: "Set to false to hide errors after validation"
|
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)
|
|> Phoenix.HTML.FormData.to_form(form.options)
|
||||||
end
|
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)
|
opts = validate_opts_with_extra_keys(opts, @validate_opts)
|
||||||
|
|
||||||
prepare_source = form.prepare_source || (& &1)
|
prepare_source = form.prepare_source || (& &1)
|
||||||
|
|
|
@ -193,7 +193,10 @@ defmodule AshPhoenix.FormTest do
|
||||||
|
|
||||||
test "blank form values unset - helps support dead view forms" do
|
test "blank form values unset - helps support dead view forms" do
|
||||||
form =
|
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"})
|
{:ok, post} = Form.submit(form, params: %{"title" => "", "text" => "bar"})
|
||||||
assert post.text == "bar"
|
assert post.text == "bar"
|
||||||
|
|
Loading…
Reference in a new issue