dev-rroslan

dev-rroslan

I am given warning here:
warning: a map with atom keys was given to a form. Maps are always considered parameters and therefore must have string keys, got: %{country_issued_id: “”}

and another warning:
warning: variable “country_issued_id” is unused (if the variable is not meant to be used, prefix it with an underscore)

Below is my code, hope someone will help me:
defmodule UndiWeb.GenerateLive do
use UndiWeb, :live_view

alias Undi.Tokens.Token

alias Undi.Tokens

@impl true
def render(assigns) do
~H"“”
<.header>
Generate Token for login to survey
<:subtitle>From ID we will obtain your age and gender</:subtitle>
</.header>

<div>
  <.simple_form for={@form} id="generate_link_form" phx-change="validate" phx-submit="save_token">
    <.input
      field={@form[:country_issued_id]}
      value={@form.params.country_issued_id}
      type="text"
      label="MyKad"
    />

    <:actions>
      <.button phx-disable-with="Generating...">Generate Token</.button>
    </:actions>
  </.simple_form>
</div>
"""

end

@impl true
def mount(_params, _session, socket) do
{:ok,
assign(socket,
form: to_form(%{country_issued_id: “”})
)}
end

@impl true
def handle_event(“save_token”, %{“country_issued_id” => country_issued_id} = p, socket) do
case Tokens.create_token(p) do
{:ok, _token} →
{
:noreply,
socket
|> put_flash(:info, “Record created successfully”)
}

  {:error, %Ecto.Changeset{} = changeset} ->
    {:noreply, assign_form(socket, changeset)}
end

end

@impl true
def handle_event(“validate”, _, socket) do
{:noreply, socket}
end

defp assign_form(socket, %Ecto.Changeset{} = changeset) do
assign(socket, :form, to_form(changeset))
end
end

Showing Posts 1 to 4

polypush135

polypush135

First issue:

Your map has atoms for keys not string literals.
IE:
You sent

%{country_issued_id: “”}

it wants

%{"country_issued_id" => “”}

Because they are parameters are possibly coming in from outside and someone could possible run out of atoms causing their app to crash. Or put another way there would be no way to limit the number of atoms the system could possibly have to make if anyone can just submit the form and pass in a new key in the form of an atom. Thus why they warning you to use strings.

Also try to look up how to use the ` char in these posts to format your code sections. It will help others read it better.

The next warning is self explanatory. You are not using the thing thus either annotate it with a prefix of _or remote it

heres you issue.

{:ok,
assign(socket,
form: to_form(%{country_issued_id: “”})
)}

should look like

{:ok,
assign(socket,
form: to_form(%{"country_issued_id" => “”})
)}

Your to_form function is taking a map that has an atom.

At the end of the day these are just warnings.

dev-rroslan

dev-rroslan OP

I am getting error:
** (KeyError) key :country_issued_id not found in: %{“country_issued_id” => “”}

polypush135

polypush135

Do you know why its saying that?

its self explaining.
I don’t even have the full context and I can see from what you said that there is no atom key of :country_issued_id found inside of this map %{“country_issued_id” => “”}

So somewhere in your code is assuming this map will have a atom key of :country_issued_id thus you should fix that since its now a string.

Also maybe go read this https://arunramgt.medium.com/elixir-basics-of-atoms-9ff6c48463d6

I personally think its very important to have a fundamental understanding of atoms and strings. This is really at the core of your issue.

Hermanverschooten

Hermanverschooten

You are accessing :country_issued_id by way of @form.params.country_issued_id, that hash doesn’t have that key. You don’t even need that value-assignment, the field above handles that already. Look at how the code in CoreComponents.input extracts the value and assigns the key, so you don’t have to.

— All posts loaded —

Where Next? Top

Trending in Questions Top

itzmidinesh
Hi all, I’m working on a Phoenix project where I need to make external HTTP requests and I’m trying to choose between Req and HTTPoison....
New
yarrichar
I’m very new to Elixir, so sorry if this is a silly question: I am wondering what happens to the mailbox of a process when the machine i...
New
pragdave
How do I tell Hologram not to escape HTML when substituting into templates? {Textile.render(@product.long_description)} escapes the HTM...
New
Fl4m3Ph03n1x
Background When trying to execute mix release on a Windows 11 machine for a Phoenix project I get the following error: * assembling mark...
New
rlipscombe
I’d like to create a file with 600 permissions (rw-------), but there’s a race condition if I create it and then change the permissions. ...
New
saleyn
Is there a way to import record definitions in the iex shell for pretty printing (similar to the rr command in the erl shell)? This woul...
New
rlipscombe
When I use Mix.install with a git dependency, using git@github.com:... how do I change the SSH identity used? I’ve got two Github accoun...
New

Other Trending Topics Top

dominicletz
Hey there, I haven’t seen a list of useful Elixir specific skills for the popular AI tools yet so I’m starting one here with my first. ...
New
raa-ziq
Hi everyone :waving_hand: I wanted to share something I’ve been building — NowBlind, a random chat app designed to help people discover ...
New
mudasobwa
I fully migrated to my own harness from Anthropic/Gemini and I think it’s time to share it. Welcome DSH, the DeepSeek Harness, fully writ...
New
KristerV
Hey. Is there anyone here who creates agents in their apps? Not talking about using agents, but creating them. I’m finding it pretty diff...
New
OndrejValenta
(I just needed to vent somewhere and LinkedIn is full of hope, or hype, I’m not sure which exactly) AI dream has many faces, but general...
New
kip
Localize is the next generation localisation library for Elixir. Think of it as ex_cldr version 3.0. The first version will be released ...
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews