krishnaoz
Ecto validation return nested response with put_assoc
ecto returning nested validation errors
"errors": {
"registration_number": [
"can't be blank"
],
"commanall": [
{
"email_id": [
"can't be blank"
],
"contacts": [
{
"contact_number": [
"can't be blank"
]
}
]
}
]
}
but we want response like
"errors": {
"registration_number": ["can't be blank"],
"email_id": ["can't be blank"],
"contact_number": ["can't be blank"]
}
this is my code
changeset_commanall = Commanall.changeset_company_contact(%Commanall{}, contact_params)
bothinsert = Ecto.Changeset.put_assoc(changeset, :commanall, [changeset_commanall])
Repo.insert(bothinsert)
First Post!
idi527
And where is the response generated? Repo.insert(bothinsert) would return an invalid changeset in case of an error. So you can flatten out the response where you transform the invalid changeset into this json, I assume?.
0
Most Liked
dimitarvp
Have you checked out Ecto.Changeset.traverse_errors? You can just collect all errors as you go and directly convert to the flat map structure you require.
2
Popular in Questions
Hi,
I am new to Elixir. I am trying to use the DateTime component to insert a date into MySQL however the there seems to be no way to fo...
New
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
When I run the Plug and I recompile I wind up having to use Ctrl C to quit iex and start again. Witht the help of rlwrap I can use the cu...
New
I believe there are people here who are dealing with CSV files import on the daily basis, and since Excel is a really popular tool there ...
New
In templates/appointment/index.html.eex:
<%= for appointment <- @appointments do %>
<tr>
<td><%= appoi...
New
As the title describes, I’m trying to run Enum.map() over a list of key/value pairs, where the value is a map. My data looks like this:
...
New
I am using the Starship cross-shell prompt – it seems pretty nice, but I get some errors:
[WARN] - (starship::utils): Executing command ...
New
Other popular topics
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
Hi guys, i’m new in the Elixir world, and i have to say, that i love it!
i’m having some problem to understand anonymous functions with ...
New
In asking this question I am more interested about the expressiveness of the language itself and less concerned about the availability of...
New
I want to highlight html closing tags when i click a html tag. That works in .html files but doesnt work for html.eex templates. How can...
New
I am trying to start a new phoenix project with elixir 1.9, but mix phx.new does not work. It says that ** (Mix) The task "phx.new" could...
New
I’ve got an issue with an app and I’ve no idea of how to troubleshoot it. I’m hoping someone here might have seen something similar.
I p...
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









