Using add_form with a many_to_many relationship

Trying to use add_form on a many_to_many relationship. Here is my action and relationship.
create :create_campaign_target_list do
primary? true
accept [:name]

argument :members, {:array, :map}, allow_nil?: true

change manage_relationship(:members, type: :append_and_remove, debug?: true)
end

many_to_many :members, MyApp.Organizations.Organization do
through MyApp.Organizations.CampaignTargetListOrganization
source_attribute_on_join_resource :campaign_target_list_id
destination_attribute_on_join_resource :organization_id
end
end
create :create_campaign_target_list do
primary? true
accept [:name]

argument :members, {:array, :map}, allow_nil?: true

change manage_relationship(:members, type: :append_and_remove, debug?: true)
end

many_to_many :members, MyApp.Organizations.Organization do
through MyApp.Organizations.CampaignTargetListOrganization
source_attribute_on_join_resource :campaign_target_list_id
destination_attribute_on_join_resource :organization_id
end
end
I using add_form to add an Organization as a member after search for one.
def handle_event("add-form", %{"path" => path, "organization_id" => organization_id}, socket) do
form =
AshPhoenix.Form.add_form(socket.assigns.form, path, params: %{id: organization_id}, type: :read)

{:noreply, assign(socket, :form, form)}
end
def handle_event("add-form", %{"path" => path, "organization_id" => organization_id}, socket) do
form =
AshPhoenix.Form.add_form(socket.assigns.form, path, params: %{id: organization_id}, type: :read)

{:noreply, assign(socket, :form, form)}
end
And the form:
<.inputs_for :let={organization} field={@form[:members]}>
<li class="list-row">
<.input field={organization[:name]} label="Name" disabled />
</li>
</.inputs_for>
<.inputs_for :let={organization} field={@form[:members]}>
<li class="list-row">
<.input field={organization[:name]} label="Name" disabled />
</li>
</.inputs_for>
If there are existing members then they will render fine and there are hidden inputs as expected, but any member I added via add_form does not have the id added as a hidden field. It is in the params. I don't want to create Organizations in the nested form only associate them via the members many_to_many relationship.
3 Replies
forest
forestOP3mo ago
The other thing I notices is that I can't even save the CampaignTargetList when I just change the name. The existing members have errors with any changes.
forms: %{
_update: #AshPhoenix.Form<
resource: MyApp.Organizations.CampaignTargetListOrganization,
action: :create,
source: #Ash.Changeset<
domain: MyApp.Organizations,
action: :create,
errors: [
%Ash.Error.Changes.Required{
field: :organization_id,
resource: MyApp.Organizations.CampaignTargetListOrganization,
class: :invalid
},
%Ash.Error.Changes.Required{
field: :campaign_target_list_id,
type: :attribute,
resource: MyApp.Organizations.CampaignTargetListOrganization,
class: :invalid
}
],
data: %MyApp.Organizations.CampaignTargetListOrganization{
forms: %{
_update: #AshPhoenix.Form<
resource: MyApp.Organizations.CampaignTargetListOrganization,
action: :create,
source: #Ash.Changeset<
domain: MyApp.Organizations,
action: :create,
errors: [
%Ash.Error.Changes.Required{
field: :organization_id,
resource: MyApp.Organizations.CampaignTargetListOrganization,
class: :invalid
},
%Ash.Error.Changes.Required{
field: :campaign_target_list_id,
type: :attribute,
resource: MyApp.Organizations.CampaignTargetListOrganization,
class: :invalid
}
],
data: %MyApp.Organizations.CampaignTargetListOrganization{
Rebecca Le
Rebecca Le3mo ago
You might find this guide useful, its not published yet - https://ash-project.github.io/ash_phoenix/forms-for-relationships-between-existing-records.html basically, you want to be working with IDs of the related records, not maps
forest
forestOP3mo ago
thank you. I will give it a try. This works and makes much more sense to work with IDs.

Did you find this page helpful?