ademenev

ademenev

I am not sure that this is specific to ash_phoenix, but that’s what I am using

ash_phoenix makes it pretty easy to build nested forms, but things get ugly pretty fast when some logic or non-standard controls appear in the form.

I have 3 resources: EntityType, Entity and Account. Entity belongs to EntityType, and Account belongs to EntityType optionally (entity_type_id in Account can be null).

I need to build a form that creates a resource call Template:

defmodule F0nance.Templates.TemplateEntry do
  use Ash.Resource, data_layer: :embedded

  actions do
    defaults [:read, :destroy, create: :*, update: :*]
  end

  attributes do
    uuid_v7_primary_key :id
    attribute :debet_entity_param, :uuid_v7, allow_nil?: true
    attribute :credit_entity_param, :uuid_v7, allow_nil?: true
  end

  relationships do
    belongs_to :debet, F0nance.Accounts.Account, allow_nil?: false, public?: true
    belongs_to :debet_entity, F0nance.Accounts.Entity, allow_nil?: true, public?: true
    belongs_to :credit, F0nance.Accounts.Account, allow_nil?: false, public?: true
    belongs_to :credit_entity, F0nance.Accounts.Entity, allow_nil?: true, public?: true
  end
end
defmodule F0nance.Templates.TemplateParam do
  use Ash.Resource, data_layer: :embedded

  actions do
    defaults [:read, :destroy, create: :*, update: :*]
  end

  validations do
    validate present(:entity_type_id) do
      where attribute_equals(:type, :entity)
    end
  end

  attributes do
    uuid_v7_primary_key :id
    attribute :name, :string, allow_nil?: false, public?: true
    attribute :type, :atom, allow_nil?: false, constraints: [one_of: [:entity]], public?: true
  end

  relationships do
    belongs_to :entity_type, F0nance.Accounts.EntityType, allow_nil?: true, public?: true
  end
end
defmodule F0nance.Templates.Template do
  alias F0nance.Templates.TemplateEntry
  alias F0nance.Templates.TemplateParam

  use Ash.Resource, domain: F0nance.Templates, data_layer: AshPostgres.DataLayer

  postgres do
    table "templates"
    repo F0nance.Repo
  end

  actions do
    defaults [:read]

    create :create do
      primary? true
      accept [:name, :description, :entries, :params]
    end
  end

  attributes do
    uuid_v7_primary_key :id
    attribute :name, :ci_string, allow_nil?: false

    attribute :description, :string
    attribute :params, {:array, TemplateParam}, allow_nil?: false, default: []
    attribute :entries, {:array, TemplateEntry}, allow_nil?: false, default: []

    create_timestamp :created_at
    update_timestamp :updated_at
  end

  identities do
    identity :name, [:name], message: "Template names must be unique"
  end
end

And this is the current prototype

One thing is that complicates things is that I am using a custom-built combo box to search for different related items (for example, accounts or entity types), and since the form only has ID of the resource that is selected, I need to make a database query to find the name. And since there is no way to put this into the form struct, I need to put it to a separate assign, and the fact that the form is nested makes the code too verbose. Another thing is that there will be more param types besides Entity, so I will need conditional display of other controls in place of Entity type. Again, the template becomes too verbose.

In journal entries sections, I also need to check if account belongs to an entity type, and if it does, I need to display a select with options listing all the params with “entity” type, and an additional option saying “fixed entity”. And again, if that “fixed entity” option is selected, an additional control has to be added to select an entity.

What I am doing is on form change I go through the nested forms and collect information about which resources need to be loaded ( “Participants” entity type and “Expenses” account in the example above) , and also generate the select options.

I think I will have to traverse the nested forms and collect and reorganize data in any case. But for the template, probably I could make a bunch of components and move all that conditional logic into the components’ modules. It’s just trading one complexity for another, but at least the HTML would be cleaner and easier to read

How do you approach similar problems?

Where Next? Top

Trending in Questions Top

katta
I having some trouble figuring out if I have set myself too strict of standards for my production server. Currently I can handle 75% of r...
New
brecabral
Documentation While reading the Scoped Routes section, I noticed that the documentation currently refers to a problem without explainin...
New
nseaSeb
Hello, I know there is an approach for handling lists that allows for optimized traversal, but I can’t recall the specific method (somet...
New
kpanic
Hi everyone, I am toying with the idea of building a “match maker” for giving personal help to people that wants to start coding. I sta...
New
velrest
So my question is quite simple and i have found no conclusive answer on forum, google or AI. Should we use :erlang.float for Integer to ...
New
asweet-confluent
I recently noticed that Elixir’s Logger defaults its primary log level to :debug when no :logger, :level application configuration is pre...
New
apz
I’m new to elixir and just tried to install the elixirLS extension for VScode(ium) and it is throwing some errors that I would like help ...
New

Other Trending Topics Top

GenericJam
Edit: 2026 May 15 - This post is archived. Mob is alive!! Main docs: mob v0.7.11 — Documentation A bit of explanation for the slightly c...
New
JesseHerrick
Hey, I’m Jesse and I’m the main contributor behind Dexter, a full-featured, lightning-fast Elixir LSP optimized for large codebases. It s...
New
mudasobwa
I am happy to introduce the very α version of the new programming language compiled to BEAM. Welcome Cure. It has literally three kille...
New
garrison
Hobbes is a low-level distributed database for the Elixir programming language. Hobbes provides a simple, safe, and scalable storage lay...
New
mhanberg
Hi everyone! The first release candidate for the Expert language server project is now available! We’ve published a press release detai...
New
budgie
A little off-topic, but I feel like people here have a good head on their shoulders. I used to be quite good at making software. Was luc...
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews