user20230119

user20230119

Nested validation for related children

How would you do nested validation for related children?

I want to validate field options under field 1 against field options in field 2.

Show validation message in field option 1 if field option 1 attribute value is not in field option 4/5/6.

  • Field 1

    • Field Option 1 - hello
      • error: value hello is not in field option 4/5/6
    • Field Option 2 - foo
    • Field Option 3 - bar
  • Field 2

    • Field Option 4 - foo
    • Field Option 5 - bar
    • Field Option 6 - baz
defmodule Helpdesk.FieldOption do
  use Ash.Resource, otp_app: :helpdesk

  actions do
    defaults [:read, create: [:name]]

    update :update do
      primary? true
      accept [:name]

      require_atomic? false
    end

  end

  attributes do
    uuid_primary_key :id

    attribute :name, :string do
      allow_nil? false
      public? true
    end

    timestamps()
  end

  relationships do
    belongs_to :form_field, Helpdesk.FormField
  end

  validations do
    validate fn changeset, context ->
      # ???
      :ok
    end
  end
end

First Post!

zachdaniel

zachdaniel

Creator of Ash

Are the users modifying the related information in the action? Because if you want to validate related data like this your best bet would likely be to do it in an after_action. i.e

defmodule ValidateChildren do
  use Ash.Resource.Change

  def change(changeset, _, _) do
    Ash.Changeset.after_action(changeset, fn changeset, result -> 
      children = Ash.load!(result, :children).children
      if something_is_wrong(...) do
       {:error, Ash.Error.set_path(Ash.Error.Invalid.InvalidChanges.exception(...), [:children, 0])}
     else
       {:ok, result}
     end
    end)
  end
end

change ValidateChildren

I’m just showing a collection of tools you have to do this kind of thing above, thats not a complete example as I’m pretty slammed at the moment :slight_smile:

Where Next?

Popular in Questions Top

vegabook
I’m brand new to Phoenix and I have stripped one of the demo applications to the bone. I just want to get an svg up on the screen. Here i...
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
joeerl
Hello again - after a longish gap I’ve decided I really must dig into Elixir and see what’s been happening here - so I have a few questio...
New
jay1
Why is it that the mnesia database isn’t the most preferred database for use in Elixir/Phoenix?
New
greenz1
I have a phoenix application from which a user can download multiple(5-6) files of size 1MB. I couldn’t find anything related to sending ...
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
fayddelight
I tried installing elixir 1.11.2 erlang 23.3.4 via asdf in my zsh shell. Enabled the versions locally and globally. When I list them ...
New

Other popular topics Top

jononomo
I am trying to figure out how Mix knows whether the environment is test, dev, or prod – where is this set? Thanks.
New
hariharasudhan94
Lets say I have map like this fetching from my database %{"_id" => #BSON.ObjectId<58eb1a7a9ad169198c3dXXXX>, "email" => ...
New
sen
Hi All, I set a environment variables in dev.exs , like below code. when i start server, how can i set the ${enable} value? thanks. d...
New
Patoshizzle
After calling mix ecto.create I get this error: 17:00:32.162 [error] GenServer #PID<0.412.0> terminating ** (Postgrex.Error) FATAL...
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 42716 114
New
AstonJ
Posting this to see if we can make things easier for people to get into Neovim. If you use Neovim and have a favourite distro please let ...
New

We're in Beta

About us Mission Statement