andym

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

  1. 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 ?
  2. 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 using user_details in 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

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

andym

thanks everyone for the explanation. this has helped me a lot in terms of understanding how this whole cycle works.

Where Next?

Popular in Questions Top

rms.mrcs
Hi, I need to transform a list of numbers into a map where the keys are the indexes and the values are the original values of the list. ...
New
minhajuddin
I have seen a lot of code which picks the first element from a list using Enum.at(0) instead of List.first. Is there a reason why people ...
New
qwerescape
Is there a way to get the call stack or stack trace at any point in the code? Not from exceptions, but an expression that returns how the...
New
joaquinalcerro
Hi there, I am working with Ecto-Postgresql and I need to call all of the records from a specific table but the table has 40,000 records...
New
hariharasudhan94
I would like to know what is the best IDE for elixir development?
New
mcarvalho
What is the difference between System.get_env and Application.get_env? For example, what are best practices to use one versus another.
New
vac
Hi, I’m quite new in Elixir and I’m trying to format a string to a PEM format. I have the certificate value like MIIDBTCCAe2...... and I...
New
JulienCorb
I am trying to implement my new.html.eex file to create new posts on my website. new.html.eex: &lt;h1&gt;Create Post&lt;/h1&gt; &lt;%= ...
New
aalberti333
As the title describes, I’m trying to run Enum.map() over a list of key/value pairs, where the value is a map. My data looks like this: ...
New
JorisKok
I have a server on AWS, and was running a load test using artillery. When looking at the Phoenix dashboard I see the Ports going to 100% ...
New

Other popular topics Top

johnnyicon
Hi all, I’ve just started learning Elixir and Phoenix Framework, so please pardon my n00bness at this stage. I’m trying to use Postgres...
New
jononomo
I am trying to figure out how Mix knows whether the environment is test, dev, or prod – where is this set? Thanks.
New
hariharasudhan94
lets say i have a sample like a = 20; b = 10; if (a &gt; b) do {:ok, "a"} end if (a &lt; b) do {:ok, b} end if (a == b) do {:ok, "equa...
New
lessless
I believe there are people here who are dealing with CSV files import on the daily basis, and since Excel is a really popular tool there ...
New
openscript
Hello! Sorry for this astonishing simple question, but I’m really stuck. I try to set up the intellij-elixir plugin, but I don’t know ho...
New
greenz1
I have a phoenix application from which a user can download multiple(5-6) files of size 1MB. I couldn’t find anything related to sending ...
New
gshaw
What is the idiomatic way of matching for not nil in Elixir? E.g., First way: defp halt_if_not_signed_in(conn, signed_in_account) when...
New
AstonJ
Seen any cool LiveView demos, sample apps or examples? Please post them here! :003:
New
aalberti333
As the title describes, I’m trying to run Enum.map() over a list of key/value pairs, where the value is a map. My data looks like this: ...
New
stefanluptak
Hello everybody, usually, I use a 29" ultra-wide monitor for VSCode which can easily accomodate explorer (files panel) + file with code ...
New

We're in Beta

About us Mission Statement