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

boundedvariable
I am going through the kafka architecture. All the features what the kafka is providing are already in Erlang. I would like hear your opi...
New
lorenzo
Hey everone! I created a prototype for my app using Nodejs for the api. But the framework I chose wasnt great (in general theresnt any g...
New
jer
I’ve been using umbrellas for a while, and generally started off (on greenfield projects at least) by isolating subapps based on clearly ...
New
lucaong
Hello Elixir and Nerves community, I have been working for a while on an open-source embedded key-value database for Elixir, that I call...
230 14362 124
New
mikl
I wanted to capitalize a string, and tried using String.capitalize(). That generally works well, until you try to capitalize a word like...
New
marciol
Please, let me know if this kind of discussion already took place in another topic . Hi all, how do you consider if is better to build ...
New
paulanthonywilson
I like Umbrella projects and pretty much always use them for personal Elixir stuff, especially Nerves things. But I don’t think this is ...
New

Other popular topics Top

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
hariharasudhan94
I would like to know what is the best IDE for elixir development?
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
greenz1
I have a phoenix application from which a user can download multiple(5-6) files of size 1MB. I couldn’t find anything related to sending ...
New
msaraiva
Surface is an experimental library built on top of Phoenix LiveView and its new LiveComponent API that aims to provide a more declarative...
564 44167 214
New

We're in Beta

About us Mission Statement