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

chrisalley
ExUnit now has describe blocks which is a welcome addition coming from RSpec. In the docs, it states that nested hierarchies of describe ...
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
SoCreat
i’m a new one to elixir which editor can i use vs code? or atom? Thanks! :smiley:
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
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
script
If I have a string “1000 cfu/ml” . I want to remove the characters and / and space . So the string is like this "1000" What is the ...
New
dblack
I’ve got an issue with an app and I’ve no idea of how to troubleshoot it. I’m hoping someone here might have seen something similar. I p...
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
Brian
What is the proper way to load a module from a file in to IEX? In the python world, doing something like this pretty standard: from ....
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

Other popular topics Top

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
sorentwo
Hello! tl;dr Announcing Oban, an Ecto based job processing library with a focus on reliability and historical observability. After spen...
985 42920 311
New
chrismccord
As promised, the first release candidate of Phoenix 1.3.0 is out! This release focuses on code generators with improved project structure...
New
jerry
Good day to you all. I have been struggling to get a query involving like and ilike to work. Can anyone assist me on this, please? pro...
New
josevalim
Hi everyone, One of the features added to Elixir early on to help integration with Erlang code was the idea of overridable function defi...
New
jay1
Why is it that the mnesia database isn’t the most preferred database for use in Elixir/Phoenix?
New
Emily
I have VueJS GUIs with the project generated using Webpack. I have Elixir modules that will need to be used by the VueJS GUIs. I forese...
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
gausby
I asked this very same question on twitter and got some interesting feedback, but I thought it would be a good question to ask here as we...
1207 39297 209
New

Latest on Elixir Forum

We're in Beta

About us Mission Statement