LiveView interface for {:array, :string} type
I'm making a LiveView interface to edit a resource with an {:array, :string} Ash type attribute and I'm looking for up-to-date, Ash-aware best practices. I've cobbled a few prototypes together but I feel like I'm missing something fundamental and getting hacky. Is there any documentation on the idiomatic way to accomplish this?
11 Replies
It depends a bit on exactly what you're trying to do. You should be able to use a combination of
AshPhoenix.Form.update_form
for adding/removing values, as well as a loop over the values. Here is some pseudocode/ideas you may be able to use.
I think there is some opportunities for utility functions there
like AshPhoenix.Form.update_param(form, "field_name", fn value -> value ++ [""] end)
that will update a param and revalidateokay sweet that's extremely essentially what I have now but with AshPhoenix.Form.update_form, which I suspect will stop my field values from vanishing on validation?
Should do
How is update_form different from set_data? I'm not really grokking it from the documentation. I'm trying to understand what I'm doing here because I keep running into things that feel inexplicable.
Set data is for modifying the underlying record of the form. Update form allows you to pick any nested form in the structure and modify it in some way.
Got it. I've gotten it working for the edit action and I'll be doing new next. I think it might be a bit of a monstrosity but I'm working my way through it 😅
defmodule GorgonWeb.HuskLive.FormComponent do
use GorgonWeb, :live_component
alias Gorgon.Forge.Husk
@impl true
def render(assigns) do
~H"""
<.header> <%= @title %> <:subtitle> {{copy}}
<.sim...
<.header> <%= @title %> <:subtitle> {{copy}}
<.sim...
usually the edit and the create can be the same form
ok cool, I think it's just angry about changes I've made for this so now I've gotta make those work with the cases where things are empty because it's new 🙂
We could probably have something like
to simplify this much more
I'll give that a shot!! I am working hard to become more simple :thinkies: