This is a simplified example of my data structure. I am trying to use get_field/3
to check the values of
- some_key_one
- some_key_two
- some_key_three
- some_key_four
If the values of the fields above are “type_a” and the value of the key type_a
is nil, then I want to return an error on the form with a message, such as “cannot choose type_a when type_a is nil”
Same logic applies for “type_b”
If the values of the fields above are “type_b” and the value of the key type_b
is nil, then I want to return an error on the form with a message, such as “cannot choose type_b when type_b is nil”
The problem is get_field/3
doesn’t accept 2 keys (at least I don’t think it does).
Using 2 arguments, I can do get_field(changeset, :list_of_things)
and that will return list_of_things [...]
Do I now need to use Enum.map
and Map.key
to traverse through every key in the list of maps checking for each key-value pair?
%System.Articles.Things{
type_a: nil,
type_b: nil,
list_of_things: [
%System.Articles.ListOfThings{
__meta__: #Ecto.Schema.Metadata<:loaded, "list_of_things">,
delete: false,
some_key_one: "type_a",
some_key_two: "empty",
some_key_three: "type_a",
some_key_four: "type_b",
...
},
%System.Articles.ListOfThings{
__meta__: #Ecto.Schema.Metadata<:loaded, "list_of_things">,
delete: false,
some_key_one: "empty",
some_key_two: "type_b",
some_key_three: "type_b",
some_key_four: "type_b",
...
}
],
updated_at: ~N[...]