earth7316

earth7316

Hi -
I’m running Phoenix 1.2.1 and Elixir 1.3.4 and having some trouble getting simple form validation to work. I’m following along in the Programming Phoenix guide and have this create function in my code

  def create(conn, %{"user" => user_params}) do
changeset = User.changeset(%User{}, user_params)
case Repo.insert(changeset) do
  {:ok, user} ->
      conn
      |> put_flash(:info, "#{user.first_name} #{user.last_name} successfully registered!   Check for a confirmation email in your inbox.")
      |> redirect(to: user_path(conn, :show, user.id))
  {:error, changeset} ->
      render(conn, "new.html", changeset: changeset, title: "Register")
end

end

When I post the form I get this when I dump out the “form” that is passed into error_tag function

[debug] %Phoenix.HTML.Form{data: %{}, errors: [], hidden: [], id: "user", impl: Phoenix.HTML.FormData.Plug.Conn, index: nil, name: "user", options: [class: "settings-form"], params: %{"email" => "", "first_name" => "", "last_name" => "", "password" => "", "password_confirmation" => ""}, source: %Plug.Conn{adapter: {Plug.Adapters.Cowboy.Conn, :...}, assigns: %{changeset: #Ecto.Changeset<action: :insert, changes: %{username: ""}, errors: [first_name: {"can't be blank", [validation: :required]}, last_name: {"can't be blank", [validation: :required]}, email: {"can't be blank", [validation: :required]}, password: {"can't be blank", [validation: :required]}, password_confirmation: {"can't be blank", [validation: :required]}], data: #Go2sci.User<>, valid?: false>, layout: {Go2sci.LayoutView, "app.html"}, title: "Register"}, .....

So when i dump out form.errors in the error_tag function it’s always but as you can see the form object has both an errors = and another one that says
errors: [first_name: {“can’t be blank”, [validation: :required]}, last_name: {“can’t be blank”, [validation: :required]}, email: {“can’t be blank”, [validation: :required]}, password: {“can’t be blank”, [validation: :required]}, password_confirmation: {“can’t be blank”, [validation: :required]}]

Not sure what’s going on? I saw this issue which briefly discusses the problem but it made it seem like it was fixed and it was from almost a year ago.

https://github.com/phoenixframework/phoenix/issues/1564

Thanks for any help

Showing Posts 1 to 2

OvermindDL1

OvermindDL1

So first let’s format that output:

%Phoenix.HTML.Form{
  data: %{},
  errors: [],
  hidden: [],
  id: "user",
  impl: Phoenix.HTML.FormData.Plug.Conn,
  index: nil,
  name: "user",
  options: [class: "settings-form"],
  params: %{
    "email" => "",
    "first_name" => "",
    "last_name" => "",
    "password" => "",
    "password_confirmation" => ""
    },
  source: %Plug.Conn{
    adapter: {Plug.Adapters.Cowboy.Conn, :...},
    assigns: %{
      changeset: #Ecto.Changeset<
        action: :insert,
        changes: %{username: ""},
        errors: [
          first_name: {"can't be blank", [validation: :required]},
          last_name: {"can't be blank", [validation: :required]},
          email: {"can't be blank", [validation: :required]},
          password: {"can't be blank", [validation: :required]},
          password_confirmation: {"can't be blank", [validation: :required]}
          ],
        data: #Go2sci.User<>,
        valid?: false
        >,
      layout: {Go2sci.LayoutView, "app.html"},
      title: "Register"
    },
    ...

So, looking at this it is now obvious that the %Phoenix.HTML.Form{} only has one errors entry, and it is indeed an empty list. The form holds information from where this information came from in its source key, thus being the same %Plug.Conn{} that is passed everywhere in your pipeline. Your %Plug.Conn{} has a key called assigns that anything, you, a plug, whatever, can store temporary information on that may be useful later on in the pipeline, in this assigns there is a user/plug-added key called errors, this one is entirely different from the errors on the %Phoenix.HTML.Form{} of course, since different objects, however this key’s value appears to have the information you are missing.

Now, this says two things to me:

  1. A put_assign/3 is called somewhere in your pipeline that is adding the :errors key, why? No clue.
  2. You are giving the form your plug instead of your changeset, you probably want to give it your changeset, not your plug. :slight_smile:

EDIT: Also, the linked github issue is entirely unrelated. :slight_smile:

earth7316

earth7316 OP

thanks OvermindDL1! I didn’t see that source being a map..
The problem was as you suspected my HTML form in the template was jacked up - I had form_for @conn instead of @changeset. That’s what I get for copy/pasting from an ePub book …

Thanks again for the detailed response!

— All posts loaded —

Where Next? Top

Trending in Questions Top

water
I’m aware of 2 such projects, are there others? git: GitHub - team-alembic/realworld: A fullstack Phoenix LiveView application with back...
New
katta
I having some trouble figuring out if I have set myself too strict of standards for my production server. Currently I can handle 75% of r...
New
peck
Hi everyone. My team and I have been working on a fairly modest app based around video streaming and chat, but we’ve landed a customer t...
New
bradley
I really like the adapter patterns that ecto, nebulex, waffle, etc. use and would love find something similar for a key management servic...
New
unaware8150
Hello folks! So at work, we are seeing some situations where we have to define some “fixed” strings that are used across the codebase in...
New
Alvinkariuki
How Can I Optimise Compile Time Dependencies I have been building an elixir application for about 2 years now. Many modules and files ha...
New

Other Trending Topics Top

budgie
A little off-topic, but I feel like people here have a good head on their shoulders. I used to be quite good at making software. Was luc...
New
KristerV
Hey. Is there anyone here who creates agents in their apps? Not talking about using agents, but creating them. I’m finding it pretty diff...
New
mudasobwa
I fully migrated to my own harness from Anthropic/Gemini and I think it’s time to share it. Welcome DSH, the DeepSeek Harness, fully writ...
New
jimsynz
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
juhalehtonen
There has been a thread to discuss the Stack Overflow Developer Survey on this forum every year since 2018, so here’s yet another one for...
New
budgie
I love Elixir. It’s one of 2 programming languages I’ve ever fallen in love with. But I don’t use it anymore. Serverless was the promis...
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews