Skip to content

Commit

Permalink
docs: document use of compound types with AshPhoenix.Form (#273)
Browse files Browse the repository at this point in the history
Co-authored-by: Allen Wyma <>
  • Loading branch information
allenwyma authored Dec 11, 2024
1 parent 1d0f216 commit 18f4772
Showing 1 changed file with 24 additions and 0 deletions.
24 changes: 24 additions & 0 deletions lib/ash_phoenix/form/form.ex
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,30 @@ defmodule AshPhoenix.Form do
|> to_form()
```
## Working with compound types
Compound types, such as `Ash.Money`, will need some extra work to make it work.
For instance, when working with the `Transfer` type in `AshDoubleEntry.Transfer`, it will have the `Ash.Money` type for `amount`. When rendering the forms, you should do as follows:
```
<.input
name={@form[:amount].name <> "[amount]"}
id={@form[:amount].id <> "_amount"}
label="Amount"
value={if(@form[:amount].value, do: @form[:amount].value.amount)}
/>
<.input
type="select"
name={@form[:amount].name <> "[currency]"}
id={@form[:amount].id <> "_currency"}
options={[:USD, :HKD, :EUR]}
label="Currency"
value={if(@form[:amount].value, do: @form[:amount].value.currency)}
/>
```
The above will allow the fields to be used by the `AshPhoenix.Form` when creating or updating a Transfer.
You can follow the same style with other compound types.
## LiveView
`AshPhoenix.Form` (unlike ecto changeset based forms) expects to be reused throughout the lifecycle of the liveview.
Expand Down

0 comments on commit 18f4772

Please sign in to comment.