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

electic
Hi, I am new to Elixir. I am trying to use the DateTime component to insert a date into MySQL however the there seems to be no way to fo...
New
baxterw3b
Hi guys, i’m new in the Elixir world, and i have to say, that i love it! i’m having some problem to understand anonymous functions with ...
New
hariharasudhan94
I would like to know what is the best IDE for elixir development?
New
jay1
Why is it that the mnesia database isn’t the most preferred database for use in Elixir/Phoenix?
New
sergio_101
I am VERY much an elixir newbie. I have taken one elixir course and one phoenix course on Udemy. During that course, I saw the instructor...
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
WestKeys
Currently suffering from paralysis by [HTTP client] analysis. This is rather unusual in Elixirland as there tends to be consensus on the ...
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
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
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
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
sergio_101
I am VERY much an elixir newbie. I have taken one elixir course and one phoenix course on Udemy. During that course, I saw the instructor...
New
TunkShif
This post is an instruction guide to help you setup your Neovim for Elixir development from scratch. It includes general information on h...
274 42576 114
New

We're in Beta

About us Mission Statement