Hi,
I have this project that I’m building as I’m learning Phoenix and I got stuck yesterday and have no idea how to fix or even approach this problem.
So, I have services and those services have orders. After the order is completed, both the seller and buyer can leave reviews. Reviews are in the same db row.
First, there is a template which contains a form
<.form :let={f} for={@review_changeset} action={ ~p"/orders/#{@order.slug}" } as="order" method="PATCH">
<%= label f, :review %>
<%= textarea f, :review %>
<%= error_tag f, :review %
<%= submit "Submit" %>
</.form>
on submit, update function is run which takes the review and then, if the user submitting the review is buyer, it updates attrs with review_buyer, and it does the same for the seller. The reason for that is that the review schema contains review_buyer and review_seller fields
def update_order(%User{} = user, %Order{} = order, %{"order" => %{"review" => review}} = attrs) do
attrs =
cond do
user.id == order.buyer_id ->
Map.put(attrs, "review", %{"review_buyer" => review})
user.id == order.seller_id ->
Map.put(attrs, "review", %{"review_seller" => review})
true ->
attrs
end
order
|> Order.review_changeset(attrs)
|> Repo.update()
Changeset
def changeset(%Review{} = review, attrs) do
review
|> cast(attrs, [:review_buyer, :review_seller])
|> validate_review()
end
defp validate_review(changeset) do
cond do
get_change(changeset, :review_buyer) ->
changeset
|> validate_length(:review_buyer, min: 10, max: 250)
get_change(changeset, :review_seller) ->
changeset
|> validate_length(:review_seller, min: 10, max: 250)
true ->
changeset
end
end
Now, this all works, but the problem is showing errors. If there is a changeset error, error is set to review_buyer or review_seller field which does not exist in the form. Form has only review field. How do I transform errors to show for review field? Or should I take a completely different approach here?