brady131313
I’m trying to create a form which has a list of nested items, each of which contain some union field. I have the following hierarchy: WorkoutTemplate has many SetGroupTemplate where SetGroupTemplate looks like the following module and both FixedReps and ClassicOverload are embedded ash resources
defmodule Weightroom.Template.SetGroupTemplate do
@moduledoc false
use Ash.Resource,
domain: Weightroom.Template,
data_layer: AshPostgres.DataLayer
alias Weightroom.Template.Progression
attributes do
uuid_primary_key :id
attribute :order, :integer, public?: true, allow_nil?: false
attribute :progression, :union do
public? true
allow_nil? false
constraints types: [
fixed_reps: [
type: Progression.FixedReps,
tag: :type,
tag_value: :fixed_reps
],
classic_overload: [
type: Progression.ClassicOverload,
tag: :type,
tag_value: :classic_overload
]
]
end
end
relationships do
belongs_to :workout, Weightroom.Template.WorkoutTemplate do
allow_nil? false
end
belongs_to :exercise, Weightroom.Journal.Exercise do
allow_nil? false
public? true
end
end
postgres do
table "set_group_templates"
repo Weightroom.Repo
references do
reference :workout, on_delete: :delete
reference :exercise, on_delete: :delete
end
custom_statements do
statement :unique_order do
up ~s|ALTER TABLE set_group_templates ADD CONSTRAINT set_group_templates_unique_order_index UNIQUE (workout_id, "order") DEFERRABLE INITIALLY DEFERRED|
down "ALTER TABLE set_group_templates DROP CONSTRAINT set_group_templates_unique_order_index"
end
end
end
actions do
defaults [:read, :destroy, create: :*]
end
When I create the form and try to add a new SetGroupTemplate I get the following error
template
|> AshPhoenix.Form.for_update(:update, forms: [auto?: true])
|> AshPhoenix.Form.add_form("form[set_groups]",
params: %{
"progression" => %{
"_union_type" => "fixed_reps"
}
}
)
** (RuntimeError) Got no "_union_type" parameter, and no union type had a tag & tag_value pair matching the params.
If you are adding a form, select a type using `params: %{"_union_type" => "type_name"}`, or if one
or more of your types is using a tag you can set that tag with `params: %{"tag" => "tag_value"}`.
Params:
%{}
Available types:
[
classic_overload: [
type: Weightroom.Template.Progression.ClassicOverload,
constraints: [on_update: :update_on_match],
tag: :type,
tag_value: :classic_overload
],
fixed_reps: [
type: Weightroom.Template.Progression.FixedReps,
constraints: [on_update: :update_on_match],
tag: :type,
tag_value: :fixed_reps
]
]
If I instead use “type” instead of “_union_type” as the key for the new progression I get the same error. Stepping through the code of ash authentication it looks like AshPhoenix.Form.Auto.determine_type/3 is called twice. The first time with the map of params and it doesn’t raise. The second time, params is empty and the function raises.
I saw there is a test for union forms here and I can’t see what I’m doing differently other than using an embedded resource for the union type instead of a simple type like string/int/etc. Any help understanding where I’m going wrong would be appreciated.
Trending in Questions
Other Trending Topics
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #library
- #deployment
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #elixirconf
- #channels
- #exunit
- #discussion
- #code-sync
- #podcasts
- #javascript
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #elixirconf-us
- #ai
- #blog-post
- #elixir-ls
- #phoenix_html
- #iex
- #graphql
- #genstage
- #websockets
- #supervisor
- #advent-of-code
- #distillery
- #processes
- #api
- #forms
- #hex
- #security
- #metaprogramming










Showing Posts 1 to 10- Show Best Posts
- Show All (oldest first)
- Show All (newest first)
zachdaniel
Hey @brady131313 we’re actually actively working on some issues around union types. Can you try
mainofash_phoenixand tell me how it goes?brady131313
@zachdaniel using
mainofash_phoenixworks as expected. I can pass the type of the union using both:_union_typeand:tagand it works, thank you.Neophen
I’m trying to follow the docs:
But trying to use this in a resource like so:
Throws error:
Didn’t want to create a new post as i think it’s part of this. I’m using
mainbranch ofash_phoenixI’ll try doing it like brady131313
Neophen
Ok figured it out,
the
has to be in a single file by itself
Same for all the others subtypes
Neophen
Ok running into an issue:
This is my actions on the page resource:
Neophen
Also is there a way to automatically add a nested ember form:
Here is my image resource:
HeaderMedia is part of a union type:
the union type is used like so in the page resource:
Here’s how i add the block:
How would i add the nested embed image form based on type?
something like:
zachdaniel
You should be able to include nested paths when you add a form, i.e
"form[foo][bar][baz]", is that what you’re looking for?Neophen
Kind of:
The blocks is an array
so when adding a new block how do i know to add a nested array to that specific index?
How would i get the
SOME_INDEXvalue?zachdaniel
Ah, I mean, I’d have to understand more about what you are specifically trying to do. Do you want it to be the case that any form that is an
imageautomatically has analtform? Or do you want the form you just added to have that nested form?If you need the former, then you’ll have to look at the
form.formsobject yourself and traverse it to figure out what forms are there and do some custom processing.For the latter, perhaps something like this?
Neophen
Works beautifully: