Thank you for your reply, excuse my brevity, my resource looks like this:
attributes do
uuid_primary_key :id
attribute :email_address, :string do
allow_nil? false
end
attribute :duration, :integer do
allow_nil? false
end
attribute :duration_unit, :duration_name do
allow_nil? false
end
calculations do
calculate :duration_option, :string, expr(duration <> " " <> duration_unit)
end
end
and in my LiveView I create the form:
def mount(_, _, socket) do
form = MyApp.MyDomain.form_to_create_my_resource()
socket = assign(socket, :fom, to_form(form)
{:ok, socket}
end
def render(assigns) do
~H"""
<.simple_form
:let={form}
for={@form}
as={:form}
id="my_resource_form"
phx-submit="save"
>
<.input field={form[:email_address]} placeholder="email" />
<.input field={form[:duration_option]} type="select" prompt="Please select" options={duration_options()} />
<:actions>
<.button type="primary">Save</.button>
</:actions>
</.simple_form>
"""
end
defp duration_options() do
[
"30 seconds": "30 seconds",
" 1 minute": "1 minute",
"5 minutes": "5 minutes"
]
def handle_event("save", %{"form" => data}, socket) do
case AshPhoenix.Form.submit(socket.assigns.form, params: data) do
{:ok, resource} ->
# handle ok case, flash and nagivate, ...
{:error, form} ->
# display form again
{:noreply, socket}
end
The error is, that duration and duration_unit are required fields.
I’m trying to use a select field with two database/resource attributes and want to populate the attributes from one virtual field.
In the pre-Ash era, I would’ve parsed the returned form value and written the result to the changeset (direction to db) or joined it for the form field value (direction from db).
The Composite Type in Ash looks exactly like that, but unfortunately AshSqlite does not seem to support those.
I guess calculations
are only for the direction from db case?