zenw0lf

zenw0lf

What's the right approach to Ecto changesets?

Hello everybody,

I’ve been trying my hand at Elixir recently. And I was wondering what’s the right approach to using Ecto changetsets in the context of later having new data to add to it.

Since it seems once a changeset has been marked as invalid because of a failed rule, it’s impossible to make it revalidate (is it possible? I couldn’t find a function for it).

So my question is as follows:

Is it the right approach to just create a new changeset and run the existing + new data through it instead of trying to add to an existing changeset?

Marked As Solved

benwilson512

benwilson512

Author of Craft GraphQL APIs in Elixir with Absinthe

That is correct!

Also Liked

zenw0lf

zenw0lf

I’m currently working with LiveView and I was storing the changeset in the socket and trying to evolve it with new changes whenever the user typed something in the form fields (which wasn’t working as expected).

Now, with all the info here what I do is reconstruct the changeset on every form change and store it in the socket to show any validation errors.

Thanks everybody!

thojanssens1

thojanssens1

You can edit the fields of the Changeset, which mark it as invalid and which are documented (:valid?, :errors, :action). But maybe you will encounter other problems doing so, depends what you want to do.

If you just set the :action to nil, the errors in the form for example disappear. Again, I don’t know precisely your goal.

See also this discussion, if you are trying to copy an existing entity for insertion: https://groups.google.com/g/elixir-ecto/c/WGm4wNA7_6I/m/pFgeBdNoAQAJ?pli=1

chouzar

chouzar

As @benwilson512 mentioned the approach you mention is ideal. But as @thojanssens1 points out it may depend on your specific case.

Probably not as idiomatic, but you’re always free to validate data and make changes to it as you see fit. For example:

def change_name(changeset, name) do
  # You may want or not check for changeset.valid? before transformation

  # Get the name or nil if is not on the changeset
  name = Ecto.Changeset.get_field(changeset, :name, nil)

  # If name is nil put the passed name parameter
  if name do
    changeset
  else 
    Ecto.Changeset.put_field(changeset, name)
  end
end

In the same way you’re also free to modify changeset’s struct by using Map.put or similar functions.

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
gshaw
What is the idiomatic way of matching for not nil in Elixir? E.g., First way: defp halt_if_not_signed_in(conn, signed_in_account) when...
New
fireproofsocks
Forgive me if this is obvious, but how does one delete a database record WITHOUT selecting it first? Ecto.Repo — Ecto v3.14.0 has exampl...
New
jay1
Why is it that the mnesia database isn’t the most preferred database for use in Elixir/Phoenix?
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
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
jason.o
In the code below, if the create action is not set to accept “extra_key” as an input, it errors out with a message shown above. Is there ...
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
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

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
Nvim
Anybody knows a comprehensive comparison of Django and Phoenix, thanks for the help. Where are they similar? Where do they differ the m...
New
jay1
Why is it that the mnesia database isn’t the most preferred database for use in Elixir/Phoenix?
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
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
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
hariharasudhan94
Lets say I have map like this fetching from my database %{"_id" => #BSON.ObjectId<58eb1a7a9ad169198c3dXXXX>, "email" => ...
New
svb
Hi! Currently I want to submit a form by pressing the Enter key. However, since my input field is of type “textarea” this is just adds a...
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

Latest on Elixir Forum

Elixir Forum

We're in Beta

About us Mission Statement