umbra

umbra

Im having a problem here… as the title suggest, my error_tag isnt working, its simply not showing the error message:

new.html.eex

<%= form_for @changeset, Routes.category_path(@conn, :create), fn f -> %>
    <label for="category_name" class="form-label">Nome da Categoria</label>
    <%= text_input f, :name, class: "form-control", placeholder: "Nome" %>
    <%= error_tag f, :name %>
    <%= submit "Cadastrar", class: "btn btn-primary my-3" %>
<% end %>

controller

def create(conn, %{"category" => category}) do
    case Records.category_create(category) do
      {:ok, _inserted_category} ->
        conn
        |> put_flash(:info, "Categoria criada!")
        |> redirect(to: Routes.category_path(conn, :index))
      {:error, changeset} ->
        conn
        |> put_flash(:error, "Erro ao criar categoria!")
        |> render("new.html", changeset: changeset)
    end
  end

context

defdelegate category_changeset(category, params), to: Category, as: :changeset
defdelegate category_create(params), to: Category, as: :create

changeset and repo action

def changeset(%Category{} = category, params \\ %{}) do
    category
    |> cast(params, [:name])
    |> validate_required([:name])
    |> validate_length(:name, min: 3, max: 15)
    |> validate_format(:name, ~r{\S})
    |> unique_constraint(:name)
  end

  def create(params) do
    %Category{}
    |> changeset(params)
    |> Repo.insert()
  end

inspected changeset
Its working properly, the error is there!

[debug] Processing with ConfinWeb.CategoryController.index/2
  Parameters: %{}
  Pipelines: [:browser]
[debug] QUERY OK source="categories" db=15.0ms idle=407.0ms
SELECT c0."id", c0."name" FROM "categories" AS c0 []

#Ecto.Changeset<
  action: :insert,
  changes: %{},
  errors: [name: {"can't be blank", [validation: :required]}],
  data: #Confin.Records.Category<>,
  valid?: false
>

HTML generated

Showing Posts 1 to 6

lleger

lleger

Is this in a LiveView? It looks like it because of the “invalid-feedback” class, which is added by LiveView when the user didn’t change the input. In your CSS that class probably has display: none or similar. This is to avoid confusing the user by adding errors to fields they haven’t changed.

Docs here: Form bindings — Phoenix LiveView v1.2.5

umbra

umbra OP

Its not LiveView :grimacing:
This is supposed to happen only with LV?

lleger

lleger

The default error_tag implementation adds the invalid-feedback class automatically. See here: phoenix/installer/templates/phx_web/views/error_helpers.ex at a55f4859aa028e7395b5b84ce0481ac4e5efb659 · phoenixframework/phoenix · GitHub

If you’re using that same function (you didn’t paste the code, so I’m not sure) then that class is being added.

Since it’s in the HTML and not showing, it’s possible you have some CSS that’s adding display: none. The default app.css Phoenix generates has this (phoenix/installer/templates/phx_assets/app.css at a55f4859aa028e7395b5b84ce0481ac4e5efb659 · phoenixframework/phoenix · GitHub) but that’s probably not activating since it doesn’t also have the phx-no-feedback class.

Which is what LV is supposed to do, as the linked docs explain. But if this isn’t a LV, then phx-no-feedback isn’t getting applied, so not sure. But I’d inspect the element in the browser to see why it’s being hidden.

umbra

umbra OP

Ok, I think that I found the problem!
Im using Bootstrap 5, and happens that it uses a class named invalid-feedback too.

Thanks! I dindt knew that Phoenix uses a class with the same name.

curioustolearn

curioustolearn

I am having this same issue with bootstrap - no errors are being displayed. I am using Phoenix Liveview. For form fields that have not yet received any input I see both phx-no-feedback and invalid-feedback classes. As the fields receive input, the phx-no-feedback is removed from them. However, the invalid-feedback class remains. Am I not seeing the errors because of this class? How do I make sure that the errors are displayed? I would really appreciate some help on this.

Update: I also could not find where Phoenix sets the styling for phx-no-feedback and invalid-feedback classes.

Thank you!

aywen

aywen

This is probably way too late, but in case someone else stumbles across this thread as well.

Possible causes of error tag not showing:

  1. Check your changeset - does it actually contain errors?
  2. Check DOM in browser and see if the field actually renders, but is hidden by invalid-feedback class… you might want to comment it out while debugging to make it easier. (The class will typically be defined in .css file in /assets/css/app.css )
  3. Check which action is set on your changeset. If it is set to :nil or :ignore, error will not be shown. (See a note on errors in Phoenix.HTML.Form). If this is the case you can either use # {_reply, changeset} = Changeset.apply_action(changeset, :insert) or change the action field manually e.g. {:noreply, assign(socket, changeset: %{changeset | action: :insert})}.
    `

Also, I’ve found a related post on the topic.

— All posts loaded —

Where Next? Top

Trending in Questions Top

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
brecabral
Documentation While reading the Scoped Routes section, I noticed that the documentation currently refers to a problem without explainin...
New
achenet
Hello, I’m trying to build a basic Phoenix web-app, and I’d like to use Tailwind. However, when I launch mix phx.server, I get an error...
New
kpanic
Hi everyone, I am toying with the idea of building a “match maker” for giving personal help to people that wants to start coding. I sta...
New
asweet-confluent
I recently noticed that Elixir’s Logger defaults its primary log level to :debug when no :logger, :level application configuration is pre...
New
Cxx-mlr
I’m working on a small exercise involving update_in/3, and I came up with this solution: data = %{ name: "Periodic Table", category:...
New
ChrisAmelia
I’ve got trouble wrapping my head around the order in which functions are called in this snippet (from Phoenix’s authentication): toke...
New

Other Trending Topics Top

GenericJam
Edit: 2026 May 15 - This post is archived. Mob is alive!! Main docs: mob v0.7.11 — Documentation A bit of explanation for the slightly c...
New
JesseHerrick
Hey, I’m Jesse and I’m the main contributor behind Dexter, a full-featured, lightning-fast Elixir LSP optimized for large codebases. It s...
New
mudasobwa
I am happy to introduce the very α version of the new programming language compiled to BEAM. Welcome Cure. It has literally three kille...
New
garrison
Hobbes is a low-level distributed database for the Elixir programming language. Hobbes provides a simple, safe, and scalable storage lay...
New
mhanberg
Hi everyone! The first release candidate for the Expert language server project is now available! We’ve published a press release detai...
New
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

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews