This is the same model I am using from my previous questions: How to model self-referential tables
In a single form, I have the parent transaction and all the children transactions (basically all the splits). However, when one of the children transactions has a validation error, it does not bubble up to the form of the child (errors
is []
) but there is a field called source:
and it shows the changeset has errors
and is not valid.
When doing a cast_assoc
how can we make sure the form for the child has the errors populated so we can show it on the UI?
One of the children forms looks like this. Note the errors: []
while the changeset showing valid?: false
%Phoenix.HTML.Form{
action: nil,
data: %App.Transaction.Transactions{
__meta__: #Ecto.Schema.Metadata<:built, "transactions">,
amount: nil,
child_transactions: #Ecto.Association.NotLoaded<association :child_transactions is not loaded>,
date: nil,
delete: nil,
has_splits: false,
id: nil,
inserted_at: nil,
parent_id: nil,
parent_transaction: #Ecto.Association.NotLoaded<association :parent_transaction is not loaded>,
temp_id: "1476de26-544f-4640-8f8e-7e68882a84c1",
updated_at: nil,
},
errors: [],
hidden: [],
id: "transactions_child_transactions_0",
impl: Phoenix.HTML.FormData.Ecto.Changeset,
index: 0,
name: "transactions[child_transactions][0]",
options: [],
params: %{
"amount" => "a",
"date" => ~D[2020-04-13],
"parent_transaction_id" => "05f8d1c9-2ed2-4994-a7fb-56c0d334c250",
"temp_id" => "1476de26-544f-4640-8f8e-7e68882a84c1",
},
source: #Ecto.Changeset<
action: nil,
changes: %{
date: ~D[2020-04-13],
effective_date: ~D[2020-04-13],
},
errors: [amount: {"is invalid", [type: :float, validation: :cast]}],
data: #App.Transaction.Transactions<>,
valid?: false
>
}
The changeset for the parent (along with the children look like this):
def changeset(transactions, attrs) do
transactions
|> cast(attrs, @params)
|> cast_assoc(:child_transactions)
|> validate_required([:amount, :date])
end
Appreciate any pointers.
Thanks!