CallumVass

CallumVass

Ecto - Conditional validate_required

Hi,

I have a question_type field which determines what part of a struct is required. If it is :text then the :text field is required, if it is :option then the :option_id field is required. However, doing what I’ve done below doesn’t seem to be working, any idea what I’m doing wrong? Thanks

  def changeset(response, attrs) do
    response
    |> cast(attrs, [:text, :question_id, :option_id, :question_type])
    |> cast_assoc(:sub_response, with: &SubResponse.changeset/2)
    |> validate_required([:question_id], message: "Please enter a value.")
    |> validate_by_type()
  end

  defp validate_by_type(changeset) do
    case get_field(changeset, :question_type) do
      :text ->
        changeset
        |> validate_required([:text], message: "Please enter a value.")

      :option ->
        changeset |> validate_required([:option_id], message: "Please enter a value.")

      _ ->
        changeset
    end
  end

Marked As Solved

al2o3cr

al2o3cr

What is the type of the question_type field in your schema? The only way it seems this could go wrong is if get_field(changeset, :question_type) isn’t returning an atom…

Last Post!

CallumVass

CallumVass

You’re right, thanks!

Where Next?

Popular in Questions 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
hariharasudhan94
Lets say I have map like this fetching from my database %{"_id" => #BSON.ObjectId<58eb1a7a9ad169198c3dXXXX>, "email" => ...
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
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
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
alice
Hey, Just curious what are the main benefits of Elixir compared to Clojure? When is Elixir more useful than Clojure and vice versa? Th...
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

Other popular topics Top

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
vonH
In asking this question I am more interested about the expressiveness of the language itself and less concerned about the availability of...
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 40082 209
New
alice
Hey, Just curious what are the main benefits of Elixir compared to Clojure? When is Elixir more useful than Clojure and vice versa? Th...
New
siddhant3030
Hi, I have to write a raw query for one of my project. But till now I have used ecto queries and don’t have much experience writing raw ...
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

We're in Beta

About us Mission Statement