hariharasudhan94

hariharasudhan94

How to add error in changeset from controller - phoenix?

How can i add changeset errors from Controller when i am trying to add error like follow

Ecto.Changeset.add_error(changeset, :old_password, "invalid current password")
render conn, "password_new.html", changeset: %{changeset | action: :insert}

error will updated in changeset.errors,

#Ecto.Changeset<action: nil, changes: %{password1: "44", password2: "44", old_password: "xxxx"}, errors: [], data: #XXXX.Password<>, valid?: true>

what mistake i made here?

Marked As Solved

wmnnd

wmnnd

Data is immutable in Elixir. This is why Ecto.Changeset.add_error/4 returns a new changeset that you need use further on:

updated_changeset = Ecto.Changeset.add_error(changeset, :old_password, "invalid current password")
render conn, "password_new.html",
       changeset: %{updated_changeset | action: :insert}

Elixir does allow the rebinding of variables so you can also write it like this:

changeset = Ecto.Changeset.add_error(changeset, :old_password, "invalid current password")
render conn, "password_new.html",
       changeset: %{changeset | action: :insert}

Also Liked

NobbZ

NobbZ

Well. Things beeing immutable, means basically that you can’t change them after creation.

So, whenever you wan’t to transform data, you have to assign the new blob of data or pass it further down the chain using a pipe (|>). If you do not do that, the last result will be “forgotten” immediately as in your first version.

Now elixir does allow to “rebind” names, so by doing var = 1; var = var + 1 it sometimes seems that my above statement isn’t true. But what really happens is, that this compiles to some code similar to this: var1 = 1; var2 = var1 + 1.

Where Next?

Popular in Questions Top

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 record...
New
dokuzbir
Hello, I am trying to convert my lists to string without losing brackets.For start i have 3 map. They look like these buyer = %{ id: ...
New
vertexbuffer
Hello, can anybody help here..? I have a list of players and I what to delete an element, but every for loop the list is reverting to ori...
New
myronmarston
The Elixir Typespec docs show the following syntax for keyword lists in typespecs: # ... | [key: type] # keyword lis...
New
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
fireproofsocks
Forgive me if this is obvious, but how does one delete a database record WITHOUT selecting it first? https://hexdocs.pm/ecto/Ecto.Repo.h...
New
romenigld
I am trying to run a deploy with docker and I successfully runned with this command: docker build -t romenigld/blog-prod . but when I t...
New
vrod
I am using the Starship cross-shell prompt – it seems pretty nice, but I get some errors: [WARN] - (starship::utils): Executing command ...
New
lanycrost
Hi everyone! I need implement if…else if…else condition from my elixir code, and anymore of this control flow structures not work proper...
New
idi527
I’ve been re-reading swift book again and noticed that multiline strings there don’t have a trailing line break, unlike in elixir iex(2)...
New

Other popular topics Top

nsuchy
Hi. I’ve noticed that Windows Powershell has it’s own IEX command and you cannot access Elixir’s IEX due to the conflict. This isn’t a cr...
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
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
vonH
When I run the Plug and I recompile I wind up having to use Ctrl C to quit iex and start again. Witht the help of rlwrap I can use the cu...
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
ovidiubadita
Hey all, I discovered Elixir and I love it. I always wanted to learn a functional programming and I intended to go for Haskell, but afte...
New
baxterw3b
Hi guys, i’m new in the Elixir world, and i have to say, that i love it! i’m having some problem to understand anonymous functions with ...
New
belgoros
I’m not a pro in using Regex and can’t figure out why the following behaviour happens, especially if we take into account the difference ...
New
Fl4m3Ph03n1x
About me? ( if you have nothing better to do than reading about some random guy in the internet :stuck_out_tongue: ) Hello all, this is ...
New
magnetic
Hey :wave:t3: Elixir community, I’ve been learning Elixir, and working on some side projects. My editor of choice is VSCode, and althoug...
New

We're in Beta

About us Mission Statement