krishnaoz

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

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?.

Most Liked

dimitarvp

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.

Where Next?

Popular in Questions Top

electic
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
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
vonH
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
lessless
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
Lily
In templates/appointment/index.html.eex: <%= for appointment <- @appointments do %> <tr> <td><%= appoi...
New
aalberti333
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
vrod
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 Top

grych
Hi folks, Few months ago I have announced the proof-of-concept of the library to manipulate the browsers DOM objects directly from Elixi...
639 54260 488
New
baxterw3b
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
vonH
In asking this question I am more interested about the expressiveness of the language itself and less concerned about the availability of...
New
dokuzbir
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
shijith.k
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
dblack
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

We're in Beta

About us Mission Statement