KiKi

KiKi

Changeset validations for data which is not inside changeset.changes

Hi guys,

I’m working on a blog and each post can have a “draft” or “published” status. In my changeset, I’ve set a changeset validation for min number of characters. I want this validation to check number of characters only if you want to publish a post.

For example, if you’re just writing a draft, it can be empty or have any number of characters but if you want to publish it, it needs to have at least 5000 characters.

I have a draft changeset but I also have a publish changeset with all the validations.

The problem is, if you save a post as draft and then you get back and want to publish it but you don’t change the post, it will be published even if it has less then 5000 characters.

The thing is, if the post is not changed, it will not be part of changeset.changes and it will skip validation so my question is; how do I force this validation on a data that’s part of Post struct and not part of changeset_changes?

Most Liked

LostKobrakai

LostKobrakai

You can use Ecto.Changeset.force_change.

Where Next?

Popular in Discussions Top

pillaiindu
In django there is a cache framework backed by memcached. Rails also puts a lot of emphasis on caching, and even the idea of russian-doll...
New
rms.mrcs
A couple of days ago I was discussing with a friend about different approaches to write microservices. He said that if he was going to w...
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
Nvim
Elixir appears to be a superior language to Python. I don’t see any advantage of Python over Elixir. Are there any?
New
Fl4m3Ph03n1x
Background A few days ago I was listening to The future of Elixir from Elixir Talks, with Dave Thomas (@pragdave ) and Brian Mitchell. I...
New
thojanssens1
It would be nice to be able to define a redirect from one route to another from the router.ex file. E.g.: redirect "/", UserController, ...
New
cvkmohan
The upcoming Phoenix 1.6 release looks very interesting. Became a habit to watch the commits - and - what they are bringing in. phx.gen...
New

Other popular topics Top

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
KronicDeth
Elixir plugin for JetBrain’s IntelliJ Platform (including Rubymine) This is a plugin that adds support for Elixir to JetBrains IntelliJ...
289 36689 110
New
jononomo
I am trying to figure out how Mix knows whether the environment is test, dev, or prod – where is this set? Thanks.
New
Qqwy
Original source of discussion: This topic on the Pragmatic Programmers’ Functional Web Development with Elixir, OTP, and Phoenix forum. ...
New
Darmani72
If I have a post route which an argument: post /my_post_route/:my_param1, MyController.my_post_handler How would get the post params ...
New
aadeshere1
I have a another noob question about loop. Since elixir is immutable, while loop is not directly possible. total = 10 while total != 0 ...
New

We're in Beta

About us Mission Statement