Ash FrameworkAF
Ash Framework5mo ago
11 replies
awestbro

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

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


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


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
Was this page helpful?