Christophx

Christophx

How to validate that at least 2 variable fields in a form are filled

How do I validate that at least 2 form fields have been filled? It doesn’t matter which ones, just as long as 2 out of n fields are filled.
I’ve gone through the answers here https://stackoverflow.com/questions/42212425/conditional-validation-in-ecto-for-or-1-of-2-fields-is-required, but I can’t seem to appropriate it to my needs.

Marked As Solved

outlog

outlog

one way would be to Enum.filter to return true for all the fields that are present? and then check that the length or count is larger than 2…

eg


Enum.filter(fields, &present?(changeset, &1)) |> length > 2

or 

Enum.filter(fields, &present?(changeset, &1)) |> Enum.count > 2

I would be very careful to call the validation with the specific fields |> validate_required_inclusion([:title , :content]) - say you add a checkbox in the future and you don’t want code to break or unexpected behaviour as that checkbox all of a sudden counts in this validation…

def validate_required_inclusion(changeset, fields) do
  if Enum.filter(fields, &present?(changeset, &1)) |> length > 2 do
    changeset
  else
    # Add the error to the first field only since Ecto requires a field name for each error.
    add_error(changeset, hd(fields), "Two of these fields must be present: #{inspect fields}")
  end
end

def present?(changeset, field) do
  value = get_field(changeset, field)
  value && value != ""
end

Where Next?

Popular in Questions Top

shahryarjb
Hello, I get Persian date from my client and convert it to normal calendar like this: def jalali_string_to_miladi_english_number(persi...
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
Harrisonl
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
vac
Hi, I'm quite new in Elixir and I'm trying to format a string to a PEM format. I have the certificate value like MIIDBTCCAe2...... and ...
New
qwerescape
Is there a way to get the call stack or stack trace at any point in the code? Not from exceptions, but an expression that returns how the...
New
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
chrisalley
ExUnit now has describe blocks which is a welcome addition coming from RSpec. In the docs, it states that nested hierarchies of describe ...
New
belgoros
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
Mooodi
Given a string, how can I get access to its character by index? Enum.at("my_string", 2) doesn't work. Or rather, not char, but a substr...
New
skosch
To my knowledge, put_in, Map.update etc. all have the one limitation of not automatically creating intermediate keys when needed (for exa...
New

Other popular topics Top

Qqwy
Update: How to use the Blogs & Podcasts section You can post links to your blog posts or podcasts either in one of the Official Blog...
3268 119930 1237
New
sorentwo
Hello! tl;dr Announcing Oban, an Ecto based job processing library with a focus on reliability and historical observability. After spen...
977 41022 311
New
senggen
Erlang/OTP 25 [erts-13.2.2] [source] [64-bit] [smp:8:8] [ds:8:8:10] [async-threads:1] 15:22:35.803 [error] gen_event {lager_file_backend...
New
sergio
In Ruby, I can go: User.find_by(email: "foobar@email.com").update(email: "hello@email.com") How can I do something similar in Elixir? ...
New
bsollish-terakeet
Credo is smart enough to check for (something like) this: assert length(the_list) == 0 with this response: Checking if an enum is empt...
New
chrismccord
This release brings a number of exciting features, including integration with the new Phoenix LiveDashboard and Phoenix LiveView. There h...
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
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
johnnyicon
Hi all, I've just started learning Elixir and Phoenix Framework, so please pardon my n00bness at this stage. I'm trying to use Postg...
New
Fl4m3Ph03n1x
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

We're in Beta

About us Mission Statement