jason.o

jason.o

AshPhoenix nested form: How to make the id of parent form resourse available to the child form

I have a nested form involving resources Task which has_many TaskMembers. Since I’m passing [%TaskMember{}] as data, the first TaskMember form type shows action_type :update (and passes validation when submitted alone), whereas from the 2nd TaskMember form and thereafter, the type becomes :create, and somehow this one causes a submit_error like the following. I believe the error is referring to the task_id required per the relationship definition in TaskMember as shown below.
I thought manage_relationship in Task’s create action will handle the passing of task id to TaskMember, but it doesn’t seem to be true in this case. What could I be missing? Thanks in advance.

    errors: [
            %Ash.Error.Changes.Required{
              field: :task_id,
              type: :attribute,
              resource: MyApp.Accounts.TaskMember,
              changeset: nil,
              query: nil,
              error_context: [],
              vars: [],
              path: [],
              stacktrace: #Stacktrace<>,
              class: :invalid
            }
          ],
 relationships do
    belongs_to :task, MyApp.Tasks.Task do
      api MyApp.Tasks
      allow_nil? false
      attribute_writable? true
    end

Live.ex

 form =
      AshPhoenix.Form.for_create(Task, :create,
        api: MyApp.Tasks,
        actor: socket.assigns.current_user,
        forms: [
          auto?: true,
          task_members: [
            resource: TaskMember,
            data: [%TaskMember{}],
            create_action: :create,
            update_action: :update,
            type: :list,
            actor: socket.assigns.current_user
          ]

  def handle_event("add_task_member", _params, socket) do
    form = AshPhoenix.Form.add_form(socket.assigns.form, :task_members)

    task_member_list = form.forms.task_members

    new_form = %{
      socket.assigns.form
      | forms: %{socket.assigns.form.forms | task_members: task_member_list}
    }

    {:noreply,
     socket
     |> assign(form: new_form)
  end

task.ex

 create :create do
  ...
     argument :task_members, {:array, :map}
      change manage_relationship(:task_members, type: :create)
    end

Marked As Solved

jason.o

jason.o

So the issue was caused by defining task_members on top of auto: true as in

 forms: [
          auto?: true,
          task_members: [
            resource: TaskMember,
            data: [%TaskMember{}],
            create_action: :create,
            update_action: :update,
            type: :list,
            actor: socket.assigns.current_user
          ]

Removing task_members: [..] fixed the issue.

Also Liked

zachdaniel

zachdaniel

Creator of Ash

In your repo, replace

  use Ecto.Repo,
    otp_app: :ash_add_form_test,
    adapter: Ecto.Adapters.Postgres

with

  use AshPostgres.Repo,
    otp_app: :ash_add_form_test

Where Next?

Popular in Questions Top

shahryarjb
Hello, I get Persian date from my client and convert it to normal calendar like this: def jalali_string_to_miladi_english_number(persi...
New
Patoshizzle
After calling mix ecto.create I get this error: 17:00:32.162 [error] GenServer #PID&lt;0.412.0&gt; terminating ** (Postgrex.Error) FATAL...
New
jaysoifer
Is there a way to rollback a specific migration and only that one (“skipping” all the other ones)? Would mix ecto.rollback -v 200809061...
New
jononomo
I am trying to figure out how Mix knows whether the environment is test, dev, or prod – where is this set? Thanks.
New
nobody
How to bind a phoenix app to a specific ip address? could not find anything about that, nowhere, unfortunately, but for me this is quite...
New
itssasanka
Hi all, Trying to get some more clarity over utc_datetime and naive_datetime for Ecto: The documentation above suggests that while ...
New
freewebwithme
Using vs code and installed ElixirLS: support and debugger. And I got an error popped up on start up says Failed to run ‘elixir’ comma...
New
shijith.k
I am trying to start a new phoenix project with elixir 1.9, but mix phx.new does not work. It says that ** (Mix) The task "phx.new" could...
New
dotdotdotPaul
Okay, I’m having a heck of a time trying to figure out how to best handle the validation of belongs_to associations in Ecto. I’m sure I’...
New
marick
I had some trouble figuring out how to make many-to-many associations work. Once I got it working, I wrote a blog post. Because I’m a nov...
New

Other popular topics Top

vertexbuffer
Hello, can anybody help here..? I have a list of players and I what to delete an element, but every for loop the list is reverting to ori...
New
AstonJ
Posting this to see if we can make things easier for people to get into Neovim. If you use Neovim and have a favourite distro please let ...
New
Patoshizzle
After calling mix ecto.create I get this error: 17:00:32.162 [error] GenServer #PID&lt;0.412.0&gt; terminating ** (Postgrex.Error) FATAL...
New
dokuzbir
I want to highlight html closing tags when i click a html tag. That works in .html files but doesnt work for html.eex templates. How can...
New
RisingFromAshes
I’ve read in another post that it may be possible with a router helper - but I couldn’t find an appropriate one, and tbh, I’m still just ...
New
jay1
Why is it that the mnesia database isn’t the most preferred database for use in Elixir/Phoenix?
New
AstonJ
We’ve put together this wiki for Phoenix LiveView - please feel free to add any info you feel is worth including. What is Phoenix LiveV...
New
marick
I had some trouble figuring out how to make many-to-many associations work. Once I got it working, I wrote a blog post. Because I’m a nov...
New
openscript
Hello! Sorry for this astonishing simple question, but I’m really stuck. I try to set up the intellij-elixir plugin, but I don’t know ho...
New
PeterCarter
There are pre-rolled solutions for other frameworks that do work. However, Phoenix does not seem to have these. Have people had good expe...
New

We're in Beta

About us Mission Statement