sergio

sergio

To_form not setting errors from the changeset errors

Here’s where I assign_form:

defp assign_form(socket, %Ecto.Changeset{} = changeset) do
    form = to_form(changeset, as: "onboarding")
    IO.inspect(form)

    if changeset.valid? do
      assign(socket, form: form, check_errors: false)
    else
      assign(socket, form: form)
    end
  end

And here’s the inspect:

%Phoenix.HTML.Form{
  source: #Ecto.Changeset<
    action: nil,
    changes: %{},
    errors: [
      first_name: {"can't be blank", [validation: :required]},
      last_name: {"can't be blank", [validation: :required]}
    ],
    data: #TestWeb.OnboardingLive.StepsSchemas.Name<>,
    valid?: false
  >,
  impl: Phoenix.HTML.FormData.Ecto.Changeset,
  id: "onboarding",
  name: "onboarding",
  data: %TestWeb.OnboardingLive.StepsSchemas.Name{
    id: nil,
    first_name: nil,
    last_name: nil
  },
  hidden: [],
  params: %{"first_name" => ""},
  errors: [], <------------- I need errors to be here, from the changeset!
  options: [method: "post"],
  index: nil,
  action: nil
}

I’m using a standalone embedded schema, with no database backing to represent a step in a wizard I’m building:

defmodule TestWeb.OnboardingLive.StepsSchemas.Name do
  use Ecto.Schema
  import Ecto.Changeset

  embedded_schema do
    field :first_name, :string
    field :last_name, :string
  end

  def changeset(name, attrs \\ %{}) do
    name
    |> cast(attrs, [:first_name, :last_name])
    |> validate_required([:first_name, :last_name])
    |> validate_length(:first_name, min: 2)
    |> validate_length(:last_name, min: 2)
  end
end

The only thing I can think of is to_form somehow expects the name of the as: to be the same as the schema? I’m not sure. Appreciate the help!

Marked As Solved

LostKobrakai

LostKobrakai

Forms render errors only if the action is set on a changeset. Otherwise it’s expected to be a fresh/untouched changeset, which you might have created by running it through SomeSchema.changeset, but the user didn’t yet have a chance to fill in.

https://github.com/phoenixframework/phoenix_ecto/blob/main/lib/phoenix_ecto/html.ex#L305-L307

Also Liked

sergio

sergio

Thank you, I could not use apply_action since it was messing up a lot of things in the way to_form handles and expects inputs. So I just manually added the action and all is well.

def handle_event("validate", %{"name" => attrs}, socket) do
  changeset =
    %Name{}
    |> Name.changeset(attrs)

  changeset = %{changeset | action: :validate}

  socket =
    socket
    |> assign_form(changeset)

  {:noreply, socket}
end
LostKobrakai

LostKobrakai

apply_action returns a schema struct, not a changeset, so that’s expected.

Where Next?

Popular in Questions Top

chokchit
** (DBConnection.ConnectionError) connection not available and request was dropped from queue after 2733ms. You can configure how long re...
New
_russellb
I want to try my hand at web scraping. What tools/libraries do I need to use. I’m hoping to turn this into something professional so don’...
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
Tee
can someone please explain to me how Enum.reduce works with maps
New
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
SoCreat
i’m a new one to elixir which editor can i use vs code? or atom? Thanks! :smiley:
New
JDanielMartinez
Hi! May someone helps me, please! I have two apps into an umbrella project: the first one is Database, which manages queries, and the se...
New
fayddelight
I tried installing elixir 1.11.2 erlang 23.3.4 via asdf in my zsh shell. Enabled the versions locally and globally. When I list them ...
New
hariharasudhan94
I would like to know what is the best IDE for elixir development?
New
vonH
In asking this question I am more interested about the expressiveness of the language itself and less concerned about the availability of...
New

Other popular topics Top

malloryerik
Hi, this is for people who, like me, have had some friction using .html.heex templates in VSCode. The solution seems to be, in a hyphena...
New
TunkShif
This post is an instruction guide to help you setup your Neovim for Elixir development from scratch. It includes general information on h...
274 42158 114
New
9mm
I am constructing a JSON object (map) and I need to conditionally set a field. I’m trying to write proper elixir-way code… and I’m at a l...
New
AstonJ
Posting this to see if we can make things easier for people to get into Neovim. If you use Neovim and have a favourite distro please let ...
New
shahryarjb
Hello, I have map which I want to convert it to string like this: the map: %{last_name: "tavakkoli", name: "shahryar"} the string I ne...
New
stefanchrobot
What’s the safe way to decode a JSON string into a struct? I want to avoid calling String.to_atom. Jason.decode can give me a map with st...
New
freewebwithme
Using vs code and installed ElixirLS: support and debugger. And I got an error popped up on start up says Failed to run ‘elixir’ comma...
New
alice
Hey, Just curious what are the main benefits of Elixir compared to Clojure? When is Elixir more useful than Clojure and vice versa? Th...
New
saif
Hello everyone, Long time lurker first time poster here. I’ve recently begun working on Elixir full-time again! :raised_hands: It’s been...
New
komlanvi
Hi everyone, I was playing with phoenix liveView but I run into an issue. I have a form and want to validate each input text when the te...
New

We're in Beta

About us Mission Statement