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.






















