augnustin

augnustin

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

Showing Posts 1 to 3

LostKobrakai

LostKobrakai

Where do you assert the parsed params and how do you trigger it (test or browser)? Generally form params are not type converted at all. Casting them with ecto is the only thing doing that.

dimitarvp

dimitarvp

You likely have something like Ecto.Changeset.cast(..., params["certification"]) in your controller, or in a plug. And it’s likely casted to an Ecto model which has these rules.

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?

— All posts loaded —

Where Next? Top

Trending in Questions Top

RSP87
I’m working on a project that simulates the bumbl example in the programming phoenix book. It acts almost like an email client. We have a...
New
nseaSeb
Hello, I know there is an approach for handling lists that allows for optimized traversal, but I can’t recall the specific method (somet...
New
brecabral
Documentation While reading the Scoped Routes section, I noticed that the documentation currently refers to a problem without explainin...
New
RemyXRenard
I’m seeing that a list inside a Kino.DataTable will be interpreted as a charlist, even if the Kino.configure() is set to charlists: :as_l...
New
velrest
So my question is quite simple and i have found no conclusive answer on forum, google or AI. Should we use :erlang.float for Integer to ...
New
asweet-confluent
I recently noticed that Elixir’s Logger defaults its primary log level to :debug when no :logger, :level application configuration is pre...
New
samoloth
Hi, I’ve just set up an application with ash_authentication. There is only magic link strategy for now, so there is no confirmation add o...
New

Other Trending Topics Top

JesseHerrick
Hey, I’m Jesse and I’m the main contributor behind Dexter, a full-featured, lightning-fast Elixir LSP optimized for large codebases. It s...
New
mudasobwa
I am happy to introduce the very α version of the new programming language compiled to BEAM. Welcome Cure. It has literally three kille...
New
marciok
Hi there! We created Gust: A task orchestrator inspired by Airflow. For those who have never heard about Aiflow, it’s a Python-based wor...
New
mhanberg
Hi everyone! The first release candidate for the Expert language server project is now available! We’ve published a press release detai...
New
jimsynz
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
Dmk
Xamal is a deployment tool for Elixir apps that deploys native releases to bare metal servers over SSH. It’s a port of GitHub - basecamp/...
New

Latest on Elixir Forum

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews