RomainT

RomainT

Hello,

I want to validates a value among possible values with validate_inclusion but I’m stuck.

My use case is an avatar generation system, layer by layer. An avatar is defined by a pack (Dragon Ball, Among Us…) and many layers (body, hat, pants…). Let’s say that you chose the Dragon ball pack that only has 3 bodies while the Among Us pack has 10. I need the enum argument of validates_inclusion(:body, enum) to depend on the pack.

Is it possible? How could I implement that?

Thanks!

Showing Posts 1 to 4

lud

lud

Is the chosen pack somewhere in the changeset?

If yes then with fetch_field you can get the pack, and from that find the right enum to pass in the validation function.

Welcome here !

RomainT

RomainT OP

Hi Lud! Thank for your reply.

The pack is indeed in the changeset.

How would you pass the right enum to validate_inclusion since it expects (as I understand it at least) a static enum.

I would love to pass a function to validate_inclusion that receives the changeset and returns an enum.

lud

lud

I dont think the enum has to be static. I think you can pass any valid enumerable.

So something like that should work:

  def changeset(changeset) do
    changeset
    |> cast(...)
    |> validate_required(...)
    |> validate_parts()
  end

  defp validate_parts(changeset) do
    pack = fetch_field!(changeset, :pack)
    
    changeset
    |> validate_inclusion(:body, Packs.parts_for(pack, :body))
    |> validate_inclusion(:pants, Packs.parts_for(pack, :pants))
  end
RomainT

RomainT OP

It works pefectly, thanks!

— All posts loaded —

Where Next? Top

Trending in Questions Top

stjefim
Hello! Suppose you are building workflow (order / task / payment) processing system with the following requirements: Each workflow con...
New
Blokh
Hey guys, I’ve got a huge CSV ( around 10 GB ) that needs to be processed hourly Do you guys have any suggestions what is the best prac...
New
roeland
Kia ora, We have been using elixir-google-api to connect to Google Drive. However, with the updates to Tesla due to CVEs this is now bro...
New
kszambelanczyk
Hello! Could someone please give me a help/sample code, how to delete a file from s3 using waffle/waffle_ecto from Phoenix app. I creat...
New
Onor.io
I have what I’ve heard referred to as a “lookup table” in my database. This is a way of assigning codes to common values. One common lo...
New
jaybe78
Hello, I’m developing a online persistent chat system (what’s app) like using elixir/dynamodb/aws for a mobile app(flutter). The diffic...
New
Trolleger
What approach to take when sending live updates to “random” users Hi! I have a question, I have a little chat app, and when I create a DM...
New

Other Trending Topics Top

garrison
Hobbes is a low-level distributed database for the Elixir programming language. Hobbes provides a simple, safe, and scalable storage lay...
New
jimsynz
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
mcass19
ExRatatui lets you cook up rich terminal UIs in Elixir, powered by Rust’s ratatui via Rustler NIFs. Build interactive terminal applicatio...
New
Damirados
Hello everyone. After busy few months I am happy to announce v0.1.0 of Emerge & Solve. They are GUI (Emerge) and State management (S...
New
netoum
Corex is an accessible, unstyled UI component library for Phoenix that integrates Zag.js state machines using Vanilla JavaScript and Live...
New
wintermeyer
There are three potential reasons for members of this forum to have a look at https://vutuv.de You are tired or annoyed of LinkedIn. Yo...
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews