andym
Liveview form help to understand
i have the following liveview form
<div>
<.form let={f} for={@changeset} phx-change="validate" phx-submit="save" phx-target={@myself}}>
<%= label f, :username %>
<%= text_input f, :username %>
<%= error_tag f, :username %>
<%= submit "Save" %>
</.form>
</div>
the corresponding .ex file for this form is
defmodule PentoWeb.Components.SubmitForm do
use PentoWeb, :live_component
use Ecto.Schema
import Ecto.Changeset
embedded_schema do
field(:username, :string)
end
def changeset(survey, attrs \\ %{}) do
survey
|> cast(attrs, [:username])
|> validate_required([
:username
])
end
def mount(socket) do
user_details = %__MODULE__{
username: "abcd"
}
change_set = changeset(user_details)
{:ok,
socket
|> assign(:user_details, user_details)
|> assign(:changeset, change_set)}
end
def update(assigns, socket) do
IO.puts("update being called")
changeset = changeset(socket.assigns.user_details)
{:ok,
socket
|> assign(assigns)
|> assign(:changeset, changeset)}
end
def handle_event("validate", params, socket) do
changeset = changeset(socket.assigns.user_details, Map.get(params, "submit_form"))
IO.inspect(changeset)
{:noreply,
socket
|> assign(:changeset, changeset)}
end
end
i am having trouble understanding how this form works, basically if someone can answer the following questions
- in the form markup what exactly does the following
for={@changeset}do ? i know it looks at a changeset, but what is it used for ? - what does the following achieve
<%= text_input f, :username %>. i know it displays the username in the textbox but how does it get the value, if you look closely at the code i’m usinguser_detailsin the mount but i’m not specifying that this is the object the form should refer to, so how does it know which object to refer to ?
Most Liked
LostKobrakai
It does not. There’s a big chance you won’t be seeing any difference in the rendered form, because the form itself only uses a subset of a changeset’s features, but consider the following:
%MyApp.Schema{}
|> cast(%{}, [:required_field])
|> validate_required([:required_field])
The resulting changeset will have an error that :required_field is not set. Now add an existing record as the data:
%MyApp.Schema{required_field: "some text"}
|> cast(%{}, [:required_field])
|> validate_required([:required_field])
The resulting changeset here won’t have an error for :required_field, because it is already set, even though there are no changes to the field.
I’d strongly suggest learning about changesets outside the contexts of forms, because they’re a really powerful tool in a wide array of places with powering forms being only one of them.
andym
thanks everyone for the explanation. this has helped me a lot in terms of understanding how this whole cycle works.
Popular in Questions
Other popular topics
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #deployment
- #library
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #channels
- #elixirconf
- #exunit
- #discussion
- #code-sync
- #javascript
- #podcasts
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #elixir-ls
- #phoenix_html
- #iex
- #blog-post
- #graphql
- #genstage
- #ai
- #websockets
- #supervisor
- #advent-of-code
- #elixirconf-us
- #distillery
- #processes
- #forms
- #api
- #metaprogramming
- #security
- #performance








