Ash FrameworkAF
Ash Framework3y ago
17 replies
roberts.gulans

select relationship

Resource relevant parts
relationships do
  belongs_to :debit_account, Budget.Account, allow_nil?: false
  belongs_to :credit_account, Budget.Account, allow_nil?: false
end

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


In live view form_component i have form where i show select to select relationship

# update
assign_new(assigns, :accounts, fn _ -> Budget.Account.list!() |> Enum.map(&{&1.subject, &1.id}) end)}

# Form preperation
AshPhoenix.Form.for_create(Budget.Payment, :create, api: Budget, actor: current_user)

# render
<.input field={{f, :credit_account_id}} type="select" prompt="Select credit account" label="Credit account" options={@accounts} />
<.input field={{f, :debit_account_id}} type="select" prompt="Select debit account" label="Debit account" options={@accounts} />


Input component
# Input preperation
def input(%{field: {f, field}} = assigns) do
  assigns
  |> assign(field: nil)
  |> assign_new(:name, fn ->
    name = Phoenix.HTML.Form.input_name(f, field)
    if assigns.multiple, do: name <> "[]", else: name
  end)
  |> assign_new(:id, fn -> Phoenix.HTML.Form.input_id(f, field) end)
  |> assign(:value, AshPhoenix.Form.value(f, field))
  |> assign(:errors, AshPhoenix.Form.errors(f, format: :simple, for_path: [])[field] |> List.wrap)
  |> input()
end


I dont understand why but selects does not hold their selected value. I can select one, but once i select other first looses value. For plain string value AshPhoenix.Form.value(f, field) returns correct input value, but for selects not.

Unrelated: for errors i had to do: AshPhoenix.Form.errors(f, format: :simple, for_path: [])[field], because AshPhoenix.Form.errors(f, format: :simple) returned %{[] => %{key => value}}

If some relevant info is missing please let me know, i will provide it.
Was this page helpful?