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
hellois not in field option 4/5/6
- error: value
- Field Option 2 - foo
- Field Option 3 - bar
- Field Option 1 - hello
-
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
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 ![]()
0
Popular in Questions
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
I have a another noob question about loop. Since elixir is immutable, while loop is not directly possible.
total = 10
while total != 0
...
New
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
Why is it that the mnesia database isn’t the most preferred database for use in Elixir/Phoenix?
New
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
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
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
I am trying to figure out how Mix knows whether the environment is test, dev, or prod – where is this set?
Thanks.
New
Lets say I have map like this fetching from my database
%{"_id" => #BSON.ObjectId<58eb1a7a9ad169198c3dXXXX>, "email" => ...
New
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
After calling mix ecto.create I get this error:
17:00:32.162 [error] GenServer #PID<0.412.0> terminating
** (Postgrex.Error) FATAL...
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
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
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #deployment
- #library
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #channels
- #elixirconf
- #exunit
- #discussion
- #code-sync
- #javascript
- #podcasts
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #elixir-ls
- #phoenix_html
- #iex
- #blog-post
- #graphql
- #genstage
- #ai
- #websockets
- #supervisor
- #elixirconf-us
- #advent-of-code
- #distillery
- #processes
- #api
- #forms
- #metaprogramming
- #security
- #hex









