tankh7
Custom changeset validations return error protocol Enumerable not implemented for #Ecto.Changeset
I’m having an issue with my custom changeset validations. There is a form where a user can change their White Labeling colors for their app. I have created custom validations that make sure the colors submitted are either hexdecimal or rgb format. Validation works OK if the value being passed to the validation doesn’t contain # or rgb in the string. If it does, the validations crash the app.
I get this error:
protocol Enumerable not implemented for '#Ecto.Changeset<action: nil, changes: %{background: "#ff", header_background: "#ff"}, errors: [background: {"Color must be in hexdecimal or rgb format", [validation: :format]}], data: #App.WhiteLabels.Theme<>, valid?: false>' of type Ecto.Changeset (a struct). This protocol is implemented for the following type(s): Ecto.Adapters.SQL.Stream, Postgrex.Stream, Floki.HTMLTree, DBConnection.PrepareStream, DBConnection.Stream, StreamData, File.Stream, Map, List, IO.Stream, Range, HashDict, Stream, HashSet, Function, Date.Range, MapSet, GenEvent.Stream
Initial changeset being passed in looks like this
#Ecto.Changeset<
action: nil,
changes: %{background: "#ff", header_background: "#ff"},
errors: [],
data: #App.WhiteLabels.Theme<>,
valid?: true
>
Here is a simplified version of my changeset code:
@spec changeset(%__MODULE__{}, map) :: %Ecto.Changeset{}
def changeset(schema, attrs) do
schema
|> cast(attrs, @fields)
|> validate_required(@required_fields)
|> validate_theme_change(:background)
|> validate_theme_change(:header_background)
end
def validate_theme_change(changeset, field) do
validate_change(changeset, field, fn (current_field, value) ->
cond do
String.contains?(value, "http") -> []
String.contains?(value, "#") -> validate_hexdecimal(changeset, field) |> IO.inspect()
String.contains?(value, "rgb") -> validate_rgb(changeset, field)
true -> [{field, "Color must be in hexdecimal or rgb format"}]
end
end
)
end
defp validate_hexdecimal(changeset, field) do
validate_format(changeset, field, ~r/^([A-Fa-f0-9]{6}|[A-Fa-f0-9]{3})$/, message: "Color must be in hexdecimal or rgb format")
end
defp validate_rgb(changeset, field) do
validate_format(changeset, field, ~r/^[Rr][Gg][Bb][\(](((([\d]{1,3})[\,]{0,1})[\s]*){3})[\)]$/, message: "Color must be in hexdecimal or rgb format")
end
Stack trace shows it gets to the line |> validate_theme_change(:background)
Marked As Solved
tankh7
Solved!
I’m returning a changeset instead of a list of errors in my validate_hexdecimal and validate_rgb functions.
1
Popular in Questions
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
can someone please explain to me how Enum.reduce works with maps
New
Hi all,
Trying to get some more clarity over utc_datetime and naive_datetime for Ecto:
https://hexdocs.pm/ecto/Ecto.Schema.html#module-...
New
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
What is most correct way to open, read and parse JSON file with poison?
For example if we have example.json file in root of some projec...
New
What’s the safe way to decode a JSON string into a struct? I want to avoid calling String.to_atom. Jason.decode can give me a map with st...
New
Could someone help me? I'm making my first elixir program, number guessing game. I can't figure out how to convert the user's guess from ...
New
Hello, I have map which I want to convert it to string like this:
the map:
%{last_name: "tavakkoli", name: "shahryar"}
the string I ne...
New
Can someone explain the settings of pool_size of Ecto in config file? and what is the recommend size?
Thanks
New
I’ve been re-reading swift book again and noticed that multiline strings there don’t have a trailing line break, unlike in elixir
iex(2)...
New
Other popular topics
Hi! May someone helps me, please!
I have two apps into an umbrella project: the first one is Database, which manages queries, and the se...
New
I have a relationship of love and hate with Elixir. Lots of things are just absolutely right, but there are some things that are kind of ...
New
Good day to you all.
I have been struggling to get a query involving like and ilike to work.
Can anyone assist me on this, please?
pro...
New
I have followed this StackOverflow post to install the specific version of Erlang.
And When I am running mix ecto.setup then getting fol...
New
Hi folks,
Few months ago I have announced the proof-of-concept of the library to manipulate the browsers DOM objects directly from Elixi...
New
As many of you may have realized by now (sorry for all the posts here) I’ve been working on a db problem where I’m trying to aggregate a ...
New
Hi. I’ve noticed that Windows Powershell has it’s own IEX command and you cannot access Elixir’s IEX due to the conflict. This isn’t a cr...
New
Hello, can anybody help here..? I have a list of players and I what to delete an element, but every for loop the list is reverting to ori...
New
None of the current solutions worked well for me, so I went ahead and built a user management system from scratch.
This project took far...
New
Hey,
Just curious what are the main benefits of Elixir compared to Clojure?
When is Elixir more useful than Clojure and vice versa?
Th...
New







