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
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…
1
Popular in Questions
What is the proper way to load a module from a file in to IEX?
In the python world, doing something like this pretty standard:
from ....
New
can someone please explain to me how Enum.reduce works with maps
New
I’m hoping you guys can give me some general advice and perhaps code examples if you’re feeling up to it.
I’m very interested in Elixir,...
New
Hi there, I'm working through my first release with elixir/phoenix. I've built a release with distillery and found that it crashes when I...
New
We have an ECS cluster with 4 services, where each task joins a single cluster, via discovery ECS discovery service.
Currently when I de...
New
I would like to know what is the best IDE for elixir development?
New
I wanted to check elixir version in phoenix because i found that my elixir is 1.5 but when i use Enum.chunk_by it said the function is un...
New
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
After calling mix ecto.create I get this error:
17:00:32.162 [error] GenServer #PID<0.412.0> terminating
** (Postgrex.Error) FATAL...
New
I will often find my self writing things similar to:
case some_value do
nil -> something()
"" -> something()
_ -> someth...
New
Other popular topics
Here are few pieces of (common) Linux knowledge that we use for reasonably small one server apps. We use Ubuntu but this should work for ...
New
I have a server on AWS, and was running a load test using artillery. When looking at the Phoenix dashboard I see the Ports going to 100% ...
New
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
i’m a new one to elixir
which editor can i use
vs code? or atom?
Thanks! :smiley:
New
I would like to know what is the best IDE for elixir development?
New
I will often find my self writing things similar to:
case some_value do
nil -> something()
"" -> something()
_ -> someth...
New
I’m not a pro in using Regex and can’t figure out why the following behaviour happens, especially if we take into account the difference ...
New
About me? ( if you have nothing better to do than reading about some random guy in the internet :stuck_out_tongue: )
Hello all, this is ...
New
This post is an instruction guide to help you setup your Neovim for Elixir development from scratch. It includes general information on h...
New
We’ve put together this wiki for Phoenix LiveView - please feel free to add any info you feel is worth including.
What is Phoenix LiveV...
New







