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

rms.mrcs
Hi, I need to transform a list of numbers into a map where the keys are the indexes and the values are the original values of the list. ...
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
vertexbuffer
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
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
Brian
What is the proper way to load a module from a file in to IEX? In the python world, doing something like this pretty standard: from ....
New
openscript
Hello! Sorry for this astonishing simple question, but I’m really stuck. I try to set up the intellij-elixir plugin, but I don’t know ho...
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

Other popular topics Top

Qqwy
Update: How to use the Blogs & Podcasts section You can post links to your blog posts or podcasts either in one of the Official Blog...
3271 130579 1222
New
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
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
chrismccord
Phoenix 1.4.0 released Phoenix 1.4 is out! This release ships with exciting new features, most notably with HTTP2 support, improved deve...
688 31525 112
New
romenigld
I am trying to run a deploy with docker and I successfully runned with this command: docker build -t romenigld/blog-prod . but when I t...
New

We're in Beta

About us Mission Statement