ash_phoenix icon indicating copy to clipboard operation
ash_phoenix copied to clipboard

passing initial params in `AshPhoenix.Form.for_create/3`

Open gmazzamuto opened this issue 1 month ago • 1 comments

Code of Conduct

  • [x] I agree to follow this project's Code of Conduct

AI Policy

  • [x] I agree to follow this project's AI Policy, or I agree that AI was not used while creating this issue.

Versions

AshPhoenix main branch

Operating system

Linux

Current Behavior

Consider these two resources:

defmodule MyApp.MyDomain.MyResource do
  use Ash.Resource,
    domain: MyApp.MyDomain,
    data_layer: Ash.DataLayer.Ets

  alias MyApp.MyDomain.MyPerson

  attributes do
    uuid_primary_key :id
    attribute :organization, :string, public?: true
    attribute :person, MyPerson, public?: true
  end
end

where MyPerson is a nested embedded resource:

defmodule MyApp.MyDomain.MyPerson do

  use Ash.Resource,
    data_layer: :embedded

  attributes do
    attribute :first_name, :string, public?: true
    attribute :last_name, :string, public?: true
  end
end

In a LiveView for creating a new resource item, AshPhoenix.Form.for_create/3 is called with some initial params for the embedded resource:

AshPhoenix.Form.for_create(my_resource, :create,
  as: "my_resource",
  params: %{person: %{first_name: "initial name"}},
  forms: [auto?: true]
)

In the LiveView, the initial form is displayed correctly with the correct initial value for the embedded resource.

The bug

Just press a key on any field and the embedded fields will disappear.

This instead works as intended

AshPhoenix.Form.for_create(my_resource, :create, as: "my_resource")
|> AshPhoenix.Form.add_form(:person, params: %{first_name: "initial name"})

Reproduction

I have created a minimal example here: https://github.com/gmazzamuto/ash_phoenix_form_issue

Just go to http://localhost:4000/resources/new and edit any field to see the bug happening.

Possibly a different bug?

As a side note, in this repo I have added two validations for both the main and the embedded resource. Both validations always return :ok, they just print a debug message. If I do

item = Ash.read_first!(MyApp.MyDomain.MyResource)
AshPhoenix.Form.for_update(item, :update, context: %{shared: %{mykey: 777777777777}})

I get:

[debug] validating person...

context #=> %Ash.Resource.Validation.Context{
  actor: nil,
  tenant: nil,
  authorize?: true,
  tracer: nil,
  message: nil,
  bulk?: false,
  source_context: %{private: %{actor: nil, authorize?: true}}
}

[debug] validating resource...

context #=> %Ash.Resource.Validation.Context{
  actor: nil,
  tenant: nil,
  authorize?: true,
  tracer: nil,
  message: nil,
  bulk?: false,
  source_context: %{
    private: %{authorize?: true},
    shared: %{mykey: 777777777777},
    mykey: 777777777777
  }
}

which shows that the context is not passed to the embedded resource.

Is this the intended behavior? By the way, the same thing happens with

Ash.Changeset.for_update(item, :update, %{person: %{first_name: "new name"}}, context: %{shared: %{mykey: 777777777777}}

Expected Behavior

No response

gmazzamuto avatar Nov 24 '25 23:11 gmazzamuto

Hm...I think the general pattern is to use add_form, but there is likely some kind of bug with how/where we apply the params? I don't have time to look into it right now, but PRs welcome!

zachdaniel avatar Nov 25 '25 01:11 zachdaniel