augnustin

augnustin

Post param parsed as integer

So I have my form with those fields POSTed:

utf8: ✓
_csrf_token: a1ILBA0eGSJSJggHJ25GYj8IIEACShkC8bgoudFN8DcUCC01EIT2Mxjl
_method: put
certification[id]: 10
certification[is_active]: true
certification[rncp_id]: 367
certification[acronym]: Diplôme
certification[level]: 7
commit: Update Certification

But on the server side, once the params are parsed, I get the following map:

%{
  id: 10,
  is_active: true,
  rncp_id: 367,
  acronym: "Diplôme",
  level: "7"
}

:id is an integer so I’m happy it was parsed, but neither :rncp_id nor :level are. Why does :rncp_id gets parsed as integer while :level correctly won’t?

I’m guessing this is because its name finishes with _id, but could this any how make sense here?

This breaks my changeset afterwards, with some: errors: [rncp_id: {"is invalid", [type: :string, validation: :cast]}] error. :frowning_face:

Why is this happening? Why such subjective rule if so? Anyway the fields are casted afterwards so it wouldn’t matter to keep it as a string.

Anyway, I’ll go for a dirty:

    params = case params[:rncp_id] do
      v when is_integer(v) -> Map.put(params, :rncp_id, Integer.to_string(v))
      _ -> params
    end

for now.

Thanks

Most Liked

al2o3cr

al2o3cr

I’d look for where this is happening - the shape you showed (atom-keyed map) isn’t the default behavior for Phoenix (string-keyed map). Maybe there’s a plug in the router or the endpoint that’s doing additional parsing?

Where Next?

Popular in Questions Top

RisingFromAshes
I’ve read in another post that it may be possible with a router helper - but I couldn’t find an appropriate one, and tbh, I’m still just ...
New
hariharasudhan94
I would like to know what is the best IDE for elixir development?
New
joeerl
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
fireproofsocks
Forgive me if this is obvious, but how does one delete a database record WITHOUT selecting it first? Ecto.Repo — Ecto v3.14.0 has exampl...
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
marius95
Hello everyone, I try to use an Javascript Event Handler in my root.html.leex file. Therefore I created a function in the app.js file: ...
New
jason.o
In the code below, if the create action is not set to accept “extra_key” as an input, it errors out with a message shown above. Is there ...
New

Other popular topics Top

KronicDeth
Elixir plugin for JetBrain’s IntelliJ Platform (including Rubymine) This is a plugin that adds support for Elixir to JetBrains IntelliJ...
289 36689 110
New
dogweather
I wrote this comment on r/haskell, and it’s not popular there. :wink: But I think I’m on to something… Haskell reminds me of Java, and e...
New
gshaw
What is the idiomatic way of matching for not nil in Elixir? E.g., First way: defp halt_if_not_signed_in(conn, signed_in_account) when...
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
alice
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
Patoshizzle
After calling mix ecto.create I get this error: 17:00:32.162 [error] GenServer #PID<0.412.0> terminating ** (Postgrex.Error) FATAL...
New

We're in Beta

About us Mission Statement