nmajor

nmajor

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.

Showing Posts 1 to 1

nmajor

nmajor OP

  @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.

— All posts loaded —

Where Next? Top

Trending in Questions Top

RSP87
I’m working on a project that simulates the bumbl example in the programming phoenix book. It acts almost like an email client. We have a...
New
RemyXRenard
I’m seeing that a list inside a Kino.DataTable will be interpreted as a charlist, even if the Kino.configure() is set to charlists: :as_l...
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
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
samoloth
Hi, I’ve just set up an application with ash_authentication. There is only magic link strategy for now, so there is no confirmation add o...
New
FlyingNoodle
If a change or preparation module uses Ash.Changeset.get_argument/2 or Ash.Query.get_argument/2 (or any of the other get_argument functio...
New
ryanwinchester
apply_graft/2 doesn’t rewrite an add_many sub-workflow’s deps on an add step. Grafted jobs cancel with “upstream job was deleted” Version...
New

Other Trending Topics Top

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
marciok
Hi there! We created Gust: A task orchestrator inspired by Airflow. For those who have never heard about Aiflow, it’s a Python-based wor...
New
jimsynz
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
Dmk
Xamal is a deployment tool for Elixir apps that deploys native releases to bare metal servers over SSH. It’s a port of GitHub - basecamp/...
New
Damirados
Hello everyone. After busy few months I am happy to announce v0.1.0 of Emerge &amp; Solve. They are GUI (Emerge) and State management (S...
New
netoum
Corex is an accessible, unstyled UI component library for Phoenix that integrates Zag.js state machines using Vanilla JavaScript and Live...
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews