nmajor

nmajor

Default hidden geo values for nested form in Ash.Phoenix

Hey! I’m loving ash so far. Please excuse me if my question is dumb. I’ve been jumping into the deep end with Ash when I’m still pretty green with Elixir and Phoenix in general.

I’m trying to figure out how to handle nested forms when I have some default starting data for a create form.

Basically I have an api from which I fetch some business data, and then I prepare that data by breaking up the API response into its business and location (a business has many locations). I’m calling this prepared initial/default data from the API the blueprint.

Then I want to render a create form with the data from the api, so the user can make any changes before submitting and saving into the database.

I’m using Form.validate(blueprint, errors: false) to set the blueprint as the initial default data for the form.

But then I find that when I submit the form to create the business object that the create fails because of missing data even though that data is included in the blueprint that I added to the form through the validate function.

I’ve learned that I can use hidden form fields to get around this but the blueprint contains a %Geo.Point{} as one of its values. I really don’t want to deal with having to serialize and deserialize the Geo.Point, I would much rather just have it use the original value from the blueprint I give to the form. It would also be cool to not have to have so many hidden form fields if I can find an easy way to make the default blueprint data stick through to the form submit.

I think transform_params might be able to help with this somehow but I haven’t been able to find a clean solution.

# lib/example_app/businesses/business.ex
defmodule ExampleApp.Businesses.Business do
  use Ash.Resource,
    otp_app: :example_app,
    domain: ExampleApp.Businesses,
    data_layer: AshPostgres.DataLayer

  actions do
    defaults [:read]

    create :create do
      primary? true
      accept [:business_name]
      argument :locations, {:array, :map}, allow_nil?: true
      change manage_relationship(:locations, type: :append_and_remove, on_no_match: :create)
    end
  end

  attributes do
    uuid_primary_key :id
    attribute :business_name, :string, allow_nil?: false
    create_timestamp :inserted_at
    update_timestamp :updated_at
  end

  relationships do
    has_many :locations, ExampleApp.Businesses.Location
  end
end
# lib/example_app/businesses/location.ex
defmodule ExampleApp.Businesses.Location do
  use Ash.Resource,
    otp_app: :example_app,
    domain: ExampleApp.Businesses,
    data_layer: AshPostgres.DataLayer

  actions do
    defaults [:read]
    create :create do
      primary? true
      accept [
        :location_name,
        :formatted_address,
        :address_line_1,
        :address_line_2,
        :city,
        :state,
        :postal_code,
        :country_code,
        :phone,
        :geo_point
      ]
    end
  end

  attributes do
    uuid_primary_key :id
    attribute :location_name, :string, allow_nil?: false
    attribute :formatted_address, :string, allow_nil?: false
    attribute :address_line_1, :string, allow_nil?: false
    attribute :address_line_2, :string, allow_nil?: true
    attribute :city, :string, allow_nil?: false
    attribute :state, :string, allow_nil?: false
    attribute :postal_code, :string, allow_nil?: false
    attribute :country_code, :string, allow_nil?: false
    attribute :phone, :string, allow_nil?: false
    attribute :geo_point, :geometry, allow_nil?: false
    create_timestamp :inserted_at
    update_timestamp :updated_at
  end

  relationships do
    belongs_to :business, ExampleApp.Businesses.Business, allow_nil?: false
  end
end
# lib/example_app/businesses.ex (domain)
defmodule ExampleApp.Businesses do
  use Ash.Domain, otp_app: :example_app, extensions: [AshPhoenix]

  forms do
    form :create_business
  end

  resources do
    resource ExampleApp.Businesses.Business do
      define :create_business, action: :create
    end

    resource ExampleApp.Businesses.Location
  end
end

LiveView (minimal – matches demo)

# lib/example_app_web/live/demo_live/business_geo_demo.ex
defmodule ExampleAppWeb.DemoLive.BusinessGeoDemo do
  use ExampleAppWeb, :live_view
  alias AshPhoenix.Form, as: Form
  alias ExampleApp.Businesses
  alias ExampleAppWeb.FormErrors

  def mount(_params, _session, socket) do
    {:ok, assign(socket, demo_form: nil, loading: false)}
  end

  def handle_params(%{"place_id" => place_id}, _uri, socket) do
    socket = assign(socket, loading: true)
    Phoenix.LiveView.start_async(socket, :demo_blueprint, fn -> fetch_blueprint(place_id) end)
  end

  def handle_async(:demo_blueprint, {:ok, blueprint}, socket) do
    form =
      Businesses.form_to_create_business(actor: socket.assigns.current_user)
      |> Form.validate(blueprint, errors: false)
      |> to_form()

    {:noreply, assign(socket, demo_form: form, loading: false)}
  end

  def render(assigns) do
    ~H"""
    <.form for={@demo_form} id="demo-business-form" phx-change="validate" phx-submit="save" class="space-y-4">
      <FormErrors.panel form={@demo_form} />
      <.input field={@demo_form[:business_name]} label="Business name" />

      <.inputs_for :let={loc} field={@demo_form[:locations]}>
        <.input field={loc[:location_name]} label="Location name" />
        <!-- Hidden fields: server-provided defaults not editable in the UI -->
        <.input type="hidden" field={loc[:formatted_address]} />
        <.input type="hidden" field={loc[:address_line_1]} />
        <.input type="hidden" field={loc[:address_line_2]} />
        <.input type="hidden" field={loc[:city]} />
        <.input type="hidden" field={loc[:state]} />
        <.input type="hidden" field={loc[:postal_code]} />
        <.input type="hidden" field={loc[:country_code]} />
        <.input type="hidden" field={loc[:phone]} />
      </.inputs_for>

      <button type="submit">Create</button>
    </.form>
    """
  end

  def handle_event("validate", %{"form" => params}, socket) do
    {:noreply, assign(socket, :demo_form, Form.validate(socket.assigns.demo_form, params))}
  end

  def handle_event("save", %{"form" => params}, socket) do
    case AshPhoenix.Form.submit(socket.assigns.demo_form, params: params) do
      {:ok, _business} -> {:noreply, push_navigate(socket, to: "/")}
      {:error, form} -> {:noreply, assign(socket, :demo_form, form)}
    end
  end

  # Simulates fetching a full business profile from an API.
  # Returns business + an array of locations, each with required fields + Geo.Point.
  defp fetch_blueprint(place_id) do
    geo = %Geo.Point{coordinates: {-111.7470373, 40.0057248}, srid: 4326}

    %{
      "business_name" => "Demo Co",
      "locations" => [
        %{
          "location_name" => "HQ",
          "formatted_address" => "12344 S Spring Lake Rd, Payson, UT 84651",
          "address_line_1" => "12344 S Spring Lake Rd",
          "address_line_2" => nil,
          "city" => "Payson",
          "state" => "UT",
          "postal_code" => "84651",
          "country_code" => "US",
          "phone" => "+1385-223-4187",
          "geo_point" => geo
        }
      ]
    }
  end
end
  • On submit, validations fail and errors show e.g.:

    • locations > [0] > geo point: is required
    • (others may surface if hidden fields are removed)
  • The server-provided nested values for geo_point aren’t preserved through submit.


Thanks in advance, and I hope there isn’t anything too obvious I’m missing.

Most Liked

nmajor

nmajor

  @impl true
  def handle_event("validate", %{"form" => params}, socket) do
    {:noreply,
     assign(
       socket,
       :demo_form,
       Form.validate(
         socket.assigns.demo_form,
         DeepMerge.deep_merge(socket.assigns.demo_form.params || %{}, params || %{})
       )
     )}
  end

  @impl true
  def handle_event("save", %{"form" => params}, socket) do
    # Expected failure: location.geo_point required but missing at submit time
    case AshPhoenix.Form.submit(socket.assigns.demo_form,
           params: DeepMerge.deep_merge(socket.assigns.demo_form.params || %{}, params || %{})
         ) do
      {:ok, _business} ->
        {:noreply, put_flash(socket, :success, "(DEMO) Created")}

      {:error, form} ->
        {:noreply, assign(socket, :demo_form, form) |> put_flash(:error, "(DEMO) Submit failed")}
    end
  end

This seems to do the trick. Doing a deep merge on validate and on save.

But I have a feeling I’m still missing something and there could be a better way.

Where Next?

Popular in Questions Top

9mm
I am constructing a JSON object (map) and I need to conditionally set a field. I’m trying to write proper elixir-way code… and I’m at a l...
New
senggen
Erlang/OTP 25 [erts-13.2.2] [source] [64-bit] [smp:8:8] [ds:8:8:10] [async-threads:1] 15:22:35.803 [error] gen_event {lager_file_backend...
New
Harrisonl
We have an ECS cluster with 4 services, where each task joins a single cluster, via discovery ECS discovery service. Currently when I de...
New
marius95
Hello everyone, I try to use an Javascript Event Handler in my root.html.leex file. Therefore I created a function in the app.js file: ...
New
qwerescape
Is there a way to get the call stack or stack trace at any point in the code? Not from exceptions, but an expression that returns how the...
New
chrisalley
ExUnit now has describe blocks which is a welcome addition coming from RSpec. In the docs, it states that nested hierarchies of describe ...
New
Lily
In templates/appointment/index.html.eex: &lt;%= for appointment &lt;- @appointments do %&gt; &lt;tr&gt; &lt;td&gt;&lt;%= appoi...
New
joeerl
Hello again - after a longish gap I’ve decided I really must dig into Elixir and see what’s been happening here - so I have a few questio...
New
Emily
I have VueJS GUIs with the project generated using Webpack. I have Elixir modules that will need to be used by the VueJS GUIs. I forese...
New
bsollish-terakeet
Credo is smart enough to check for (something like) this: assert length(the_list) == 0 with this response: Checking if an enum is empt...
New

Other popular topics Top

skosch
To my knowledge, put_in, Map.update etc. all have the one limitation of not automatically creating intermediate keys when needed (for exa...
New
gshaw
What is the idiomatic way of matching for not nil in Elixir? E.g., First way: defp halt_if_not_signed_in(conn, signed_in_account) when...
New
minhajuddin
I have seen a lot of code which picks the first element from a list using Enum.at(0) instead of List.first. Is there a reason why people ...
New
Fl4m3Ph03n1x
About me? ( if you have nothing better to do than reading about some random guy in the internet :stuck_out_tongue: ) Hello all, this is ...
New
msaraiva
Surface is an experimental library built on top of Phoenix LiveView and its new LiveComponent API that aims to provide a more declarative...
564 43757 214
New
jerry
Good day to you all. I have been struggling to get a query involving like and ilike to work. Can anyone assist me on this, please? pro...
New
Lily
In templates/appointment/index.html.eex: &lt;%= for appointment &lt;- @appointments do %&gt; &lt;tr&gt; &lt;td&gt;&lt;%= appoi...
New
komlanvi
Hi everyone, I was playing with phoenix liveView but I run into an issue. I have a form and want to validate each input text when the te...
New
joaquinalcerro
Hi there, I am working with Ecto-Postgresql and I need to call all of the records from a specific table but the table has 40,000 records...
New
vegabook
I’m brand new to Phoenix and I have stripped one of the demo applications to the bone. I just want to get an svg up on the screen. Here i...
New

Latest on Elixir Forum

Elixir Forum

We're in Beta

About us Mission Statement