I have managed to list the records from my DB to the front end but not able to update , insert and apply form validation error please suggest
http://localhost:4000/admin/income-cards/1/edit
Has form as shown below:
<%= form_for @changeset, “/admin/income-cards/#{card.id}”, [method: “put”], fn f -> %>
<%= textarea f, :“card_description”, id: “card_description”, value: “#{card.card_description}”, rows: “30” , columns: “30” %>
<%= error_tag f, :card_description %>
<%= text_input f, :“card_income”, id: “card_income”, placeholder: “Name”, value: “#{card.card_income}” %>
<%= error_tag f, :card_income %>
<%= text_input f, :“card_expense”, id: “card_expense”, placeholder: “Name”, value: “#{card.card_expense}” %>
<%= error_tag f, :card_expense %>
<% end %>
The function to shown above form in my controller is
def edit_income_card(conn, %{“id” => id}) do
IO.inspect id
query = from c in GameOfFortune.Cards.Card,
where: c.id == 1,
order_by: [
asc: c.id
],
select: struct(
c,
[:id, :card_type_id, :card_description, :card_income, :card_expense]
)
cards = GameOfFortune.Repo.all(query)
IO.inspect cards
changeset = Card.changeset(Enum.at(cards, 0), %{} )
render(conn, “editcard.html”, [cards: cards, changeset: changeset])
end
def update_income_card(conn, %{“id”=>id,“card” => card_params}) do
IO.inspect “TESTING 123”
IO.inspect card_params
IO.inspect id
#IO.inspect cards_params
conn |> put_flash(:error, “Oops some validation error.”) |> redirect(to: Routes.admin_path(conn, :edit_income_card, id))
end
My routes are as shown below:
show_income_cards_path GET /admin/income-cards
GameOfFortuneWeb.AdminController :show_income_cards
admin_path GET /admin/income-cards/:id/edit
GameOfFortuneWeb.AdminController :edit_income_card
admin_path PUT /admin/income-cards/:id
GameOfFortuneWeb.AdminController :update_income_card
admin_path PATCH /admin/income-cards/:id
GameOfFortuneWeb.AdminController :update_income_card
What changeset should I pass in my edit_income_card function so that if user left any field any field blank like “description” or “income” or “expense” then I want to show validation error also if no validation error then I have to pass update the value to my database using the function update_income_card() which using PUT method as shown in my routes above.
Not clear what code should I write to validate and to update data to the database
Please suggest how to apply changeset for update action, insert action and for validation