Ash FrameworkAF
Ash Framework6mo ago
5 replies
forest

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


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


And the form:

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