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
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
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








