-
-
Notifications
You must be signed in to change notification settings - Fork 66
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
100 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,7 +4,7 @@ defmodule AshPhoenix.FormTest do | |
import ExUnit.CaptureLog | ||
|
||
alias AshPhoenix.Form | ||
alias AshPhoenix.Test.{Api, Comment, OtherApi, Post, PostWithDefault} | ||
alias AshPhoenix.Test.{Api, Author, Comment, OtherApi, Post, PostWithDefault} | ||
alias Phoenix.HTML.FormData | ||
|
||
describe "validate_opts" do | ||
|
@@ -772,6 +772,45 @@ defmodule AshPhoenix.FormTest do | |
assert [nested_form] = inputs_for(form, :post) | ||
assert nested_form.errors == [{:text, {"is required", []}}] | ||
end | ||
|
||
test "errors with a path are propagated down to the appropirate nested form" do | ||
author = %Author{ | ||
email: "[email protected]" | ||
} | ||
|
||
form = | ||
author | ||
|> Form.for_update(:update_with_embedded_argument, api: Api, forms: [auto?: true]) | ||
|> Form.add_form(:embedded_argument, params: %{}) | ||
|> Form.validate(%{"embedded_argument" => %{"value" => "[email protected]"}}) | ||
|> form_for("action") | ||
[nested_form] = inputs_for(form, :embedded_argument) | ||
|
||
|
||
# This is the top level error with a path to the nest form. | ||
assert [ | ||
%Ash.Error.Changes.InvalidArgument{ | ||
field: :value, | ||
message: "must match email", | ||
value: "[email protected]", | ||
path: [:embedded_argument], | ||
class: :invalid | ||
} | ||
] = form.source.source.errors | ||
assert form.errors == [] | ||
|
||
# This is the error on the nested form. | ||
assert [ | ||
%Ash.Error.Changes.InvalidArgument{ | ||
field: :value, | ||
message: "must match email", | ||
value: "[email protected]", | ||
path: [], | ||
class: :invalid | ||
} | ||
] = nested_form.source.source.errors | ||
assert nested_form.errors == [{:value, {"must match email", []}}] | ||
end | ||
end | ||
|
||
describe "data" do | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
defmodule AshPhoenix.Test.EmbeddedArgument do | ||
@moduledoc false | ||
use Ash.Resource, data_layer: :embedded | ||
|
||
attributes do | ||
attribute :value, :string | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
defmodule AshPhoenix.Test.ValidateEmbeddedArgument do | ||
@moduledoc """ | ||
This is a contrived example, but we want to validate one or more arguments' attributes | ||
against an attribute on the parent resource and then put an error on the | ||
changeset that will get propogated down to the nest form for the embedded argument. | ||
""" | ||
|
||
use Ash.Resource.Validation | ||
|
||
# This is the name of our embedded argument | ||
@embedded_argument :embedded_argument | ||
|
||
# This is the name of the attribute on the embedded argument | ||
@embedded_attribute :value | ||
|
||
@impl true | ||
def validate(changeset, _opts) do | ||
case Ash.Changeset.get_argument(changeset, @embedded_argument) do | ||
nil -> | ||
:ok | ||
|
||
argument -> | ||
apply_validation(changeset, argument) | ||
end | ||
end | ||
|
||
def apply_validation(changeset, argument) do | ||
email = Ash.Changeset.get_attribute(changeset, :email) | ||
value = Map.get(argument, @embedded_attribute) | ||
|
||
if value == email do | ||
:ok | ||
else | ||
{:error, | ||
Ash.Error.Changes.InvalidArgument.exception( | ||
field: @embedded_attribute, | ||
message: "must match email", | ||
value: value, | ||
path: [@embedded_argument] | ||
) | ||
} | ||
end | ||
end | ||
end |