AshPhoenix nested form not populating

Hello! I ran into an issue with generating nested forms. I've read through the nested forms docs: https://hexdocs.pm/ash_phoenix/nested-forms.html#defining-the-structure but I can't seem to create a nested form for an optional has_one relationship. I have a resource Submission:
defmodule Feedback.Submission do
...
actions do
create :create_with_submitter do
primary? true
accept [
...
]
argument :submitter, :map do
allow_nil? true
end
change manage_relationship(:submitter, type: :create)
end
end
multitenancy do
strategy :context
end
relationships do
has_one :submitter, Feedback.Submitter, destination_attribute: :submission_id
end
end
defmodule Feedback.Submission do
...
actions do
create :create_with_submitter do
primary? true
accept [
...
]
argument :submitter, :map do
allow_nil? true
end
change manage_relationship(:submitter, type: :create)
end
end
multitenancy do
strategy :context
end
relationships do
has_one :submitter, Feedback.Submitter, destination_attribute: :submission_id
end
end
and Submitter:
defmodule Feedback.Submitter do
use Ash.Resource
actions do
create :create do
primary? true
accept [
...
]
end
end
multitenancy do
strategy :context
end
relationships do
belongs_to :submission, Feedback.Submission, source_attribute: :submission_id
end
end
defmodule Feedback.Submitter do
use Ash.Resource
actions do
create :create do
primary? true
accept [
...
]
end
end
multitenancy do
strategy :context
end
relationships do
belongs_to :submission, Feedback.Submission, source_attribute: :submission_id
end
end
I've defined the domain extension:
defmodule Feedback do
use Ash.Domain,
extensions: [AshPhoenix]

forms do
form :create_submission
end

resources do
resource Feedback.Submission do
define :create_submission, action: :create_with_submitter
end
resource Feedback.Submitter
end
end
defmodule Feedback do
use Ash.Domain,
extensions: [AshPhoenix]

forms do
form :create_submission
end

resources do
resource Feedback.Submission do
define :create_submission, action: :create_with_submitter
end
resource Feedback.Submitter
end
end
I've tried calling the form generator automatically and manually, but I still can't seem to get the nested form to load. It always comes back as not loaded in the form source.data, and no keys under the form either. I can work around this by loading the forms separately and validating each. Any tips on loading the nested submitter form?
Solution:
You have to add a form using AshPhoenix.Form.add_form
Jump to solution
6 Replies
awestbro
awestbroOP4w ago
Thing's I've tried:
form = Feedback.form_to_create_submission()
# OR
form = AshPhoenix.Form.for_create(Feedback.Submission, :create_with_submitter,
forms: [
submitter: [
type: :single,
resource: Feedback.Submitter,
create_action: :create
]
]
)
IO.inspect(form)
# Prints
AshPhoenix.Form<
resource: Feedback.Submission,
action: :create_with_submitter,
type: :create,
params: %{},
source: #Ash.Changeset<
domain: Feedback,
action_type: :create,
action: :create_with_submitter,
attributes: %{},
relationships: %{},
arguments: %{},
errors: [
...
],
data: %Feedback.Submission{
id: nil,
...
submitter: #Ash.NotLoaded<:relationship, field: :submitter>,
__meta__: #Ecto.Schema.Metadata<:built, "submissions">
},
valid?: false
>,
name: "form",
data: nil,
form_keys: [
submitter: [
type: :single,
resource: Feedback.Submitter,
create_action: :create
]
],
forms: %{},
domain: Feedback,
method: "post",
submit_errors: nil,
id: "form",
transform_errors: nil,
original_data: nil,
transform_params: nil,
prepare_params: nil,
prepare_source: nil,
raw_params: %{},
warn_on_unhandled_errors?: true,
any_removed?: false,
added?: false,
changed?: false,
touched_forms: MapSet.new([]),
valid?: false,
errors: nil,
submitted_once?: false,
just_submitted?: false,
...
>
form = Feedback.form_to_create_submission()
# OR
form = AshPhoenix.Form.for_create(Feedback.Submission, :create_with_submitter,
forms: [
submitter: [
type: :single,
resource: Feedback.Submitter,
create_action: :create
]
]
)
IO.inspect(form)
# Prints
AshPhoenix.Form<
resource: Feedback.Submission,
action: :create_with_submitter,
type: :create,
params: %{},
source: #Ash.Changeset<
domain: Feedback,
action_type: :create,
action: :create_with_submitter,
attributes: %{},
relationships: %{},
arguments: %{},
errors: [
...
],
data: %Feedback.Submission{
id: nil,
...
submitter: #Ash.NotLoaded<:relationship, field: :submitter>,
__meta__: #Ecto.Schema.Metadata<:built, "submissions">
},
valid?: false
>,
name: "form",
data: nil,
form_keys: [
submitter: [
type: :single,
resource: Feedback.Submitter,
create_action: :create
]
],
forms: %{},
domain: Feedback,
method: "post",
submit_errors: nil,
id: "form",
transform_errors: nil,
original_data: nil,
transform_params: nil,
prepare_params: nil,
prepare_source: nil,
raw_params: %{},
warn_on_unhandled_errors?: true,
any_removed?: false,
added?: false,
changed?: false,
touched_forms: MapSet.new([]),
valid?: false,
errors: nil,
submitted_once?: false,
just_submitted?: false,
...
>
Solution
ZachDaniel
ZachDaniel4w ago
You have to add a form using AshPhoenix.Form.add_form
ZachDaniel
ZachDaniel4w ago
The form doesn't assume that you want to fill that field out by default
awestbro
awestbroOP4w ago
Ah, is that because it's not a required relationship? The line from the docs: AshPhoenix.Form automatically infers what "nested forms" are available, based on introspecting actions which use change manage_relationship. For example, in the following action: ... made me think it would embed the form automatically. I especially thought it would do so when I manually defined it
ZachDaniel
ZachDaniel4w ago
Right so it informs the configuration of the forms I.e what resource and actions they correspond to etc It doesn't add an empty instance of the forms regardless of the argument being required or not
awestbro
awestbroOP4w ago
Oh I see. Thanks for the clarification!

Did you find this page helpful?